简体   繁体   中英

bindValue() in SQL statement throws exception

I am trying to bind values to this SQL statement:

    $sQuery = $db->prepare('SELECT * FROM products WHERE name = "Hello" AND category = :sCategory AND material = 3 AND size = 20');
    $sQuery->bindValue(':sName', $_POST["txtName"]);
    $sQuery->bindValue(':sCategory', 5);
    $sQuery->bindValue(':sMaterial', $_POST["txtMaterial"]);
    $sQuery->bindValue(':sSize', $_POST["txtSize"]);
    $sQuery->execute();
    $aOrders = $sQuery->fetchAll();

But it keeps throwing an exception when I use the placeholder (:sCategory in this code). It runs if I just put the 5 directly in the statement, but not if I do it like this. Can anyone help me out to why that could be?

$sQuery = $db->prepare('SELECT * FROM products WHERE name = :sName AND category = :sCategory AND material = :sMaterial AND size = :sSize');
$sQuery->bindValue(':sName', $_POST["txtName"]);
$sQuery->bindValue(':sCategory', 5);
$sQuery->bindValue(':sMaterial', $_POST["txtMaterial"]);
$sQuery->bindValue(':sSize', $_POST["txtSize"]);
$sQuery->execute();
$aOrders = $sQuery->fetchAll();

I understand you said you were testing each bindValue one at a time. But in order to do that, you'll have to delete the bindValue. To "test", try this:

$sQuery = $db->prepare('SELECT * FROM products WHERE name = "Hello" AND category = :sCategory AND material = 3 AND size = 20');
$sQuery->bindValue(':sCategory', 5);
$sQuery->execute();
$aOrders = $sQuery->fetchAll();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM