php
  • mysql
  • pdo
  • 2014-07-11 70 views -1 likes 
    -1

    我怎樣寫這樣的事情與PDO?:我怎樣寫有多個值的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')); 
    

    我m不完全確定如何設置它在哪裏:trx_state同時具有值​​3和4.

    +3

    'trx_state IN(:state_1,:state_2)'然後綁定到那些? – tadman

    回答

    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只是變量,可以命名爲任何你想要的。

    相關問題