简体   繁体   English

SQL 语句中的 bindValue() 抛出异常

[英]bindValue() in SQL statement throws exception

I am trying to bind values to this SQL statement:我正在尝试将值绑定到此 SQL 语句:

    $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).但是当我使用占位符(此代码中的 :sCategory )时,它不断抛出异常。 It runs if I just put the 5 directly in the statement, but not if I do it like this.如果我只是将 5 直接放在语句中,它就会运行,但如果我这样做的话,它就不会运行。 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.我知道你说你一次测试每个 bindValue。 But in order to do that, you'll have to delete the bindValue.但是为了做到这一点,您必须删除 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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