如何使用PDO编写类似的内容?:
$query = "SELECT *
FROM trx
WHERE item_id = $item_id
AND NOT (trx_state=3 OR trx_state=4)
这就是我所拥有的:
$query = 'SELECT *
FROM trx
WHERE item_id = :item_id AND NOT (trx_state = :trx_state OR trx_state = :trx_state';
$stmt = $db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$stmt->execute(array(':item_id' => $item_id, ':trx_state' => '3', '4'));
我不确定如何设置它:trx_state的值为3和4。
答案 0 :(得分:1)
:trx_state
不必匹配您要与之比较的值,因此您可以将代码更改为:
$query = 'SELECT *
FROM trx
WHERE item_id = :item_id AND NOT (trx_state = :trx_state_1 OR trx_state = :trx_state_2';
$stmt = $db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$stmt->execute(array(':item_id' => $item_id, ':trx_state_1' => '3', ':trx_state_2' => '4'));
基本上:trx_state
只是变量,可以根据您的喜好命名。