$sql = "SELECT message FROM sale WHERE seller_id=? && payment_date=? && bank=?";
$q = $conn->prepare($sql);
$result = $q->execute(array($user_id, $paydate, bank1|| bank2|| bank3));
如何纠正“bank1 || bank2 || bank3”的错误?
$sql = "SELECT message FROM sale WHERE seller_id=? AND payment_date=?
AND bank IN (?, ?, ?)";
$q = $conn->prepare($sql);
$result = $q->execute(array($user_id, $paydate, $bank1, $bank2 $bank3));
如果您有动态数量的银行(例如在数组 $banks 中):
$sql = "SELECT message FROM sale WHERE seller_id=? AND payment_date=? ";
$values = array($user_id, $paydate);
if ($banks) {
$sql .= " AND bank IN (" . implode(",",array_fill(0,count($banks),"?")) . ")";
$values = array_merge($values, array_values($banks));
}
$q = $conn->prepare($sql);
$result = $q->execute($values);
PS:我建议在 SQL 中使用AND
andOR
而不是&&
and ||
。在标准 SQL 中,||
是字符串连接运算符。明智的做法是尽可能养成编写标准 SQL 的习惯。
将银行保存在数组中,然后将数组放在执行语句中。但您也必须更改&& bank=
为&& bank in ()
,因为您要查询的银行数量是动态的。