【问题标题】:PDO multiple named placeholders doesnt retrieve dataPDO 多个命名占位符不检索数据
【发布时间】:2012-11-20 00:10:24
【问题描述】:

如果准备好的语句只有一个命名占位符,我到目前为止编写的代码可以正常工作,但如果查询有多个条件,它不会从数据库返回任何结果。

例如:

$query = array();
$query['columns'] = array('*');
$query['tables'] = array('esl_comments');
$query['where'] = array(
    'esl_comments.commentVisible' => array('=', 'Y')
);

工作正常。但如果我尝试:

$query = array();
$query['columns'] = array('*');
$query['tables'] = array('esl_comments');
$query['where'] = array(
    'esl_comments.commentVisible' => array('=', 'Y'),
    'esl_comments.commentID' => array('=', '1'),
);

(注意附加的commentID参数)尽管mySQL数据库中有满足条件的数据,但它无法返回任何内容。

我写的PDO代码是:

$sql ='SELECT ';
                foreach($query['columns'] as $column){ //What columnns do we want to fetch?
                    $sql.=$column . ", ";
                }
                $sql = rtrim($sql, " ,");
                $sql .=' FROM '; //Which tables will we be accessing?
                foreach($query['tables'] as $tables){
                    $sql.=$tables . ", ";
                }
                $sql = rtrim($sql, " ,"); //Get rid of the last comma
                $sql .=' WHERE ';

                if(array_key_exists('where', $query)) //check if a where clause was provided
                {
                    $fieldnames = array_keys($query['where']);
                    $count = 0;
                    $size = sizeof($fieldnames);
                    $bindings = array();
                    foreach($query['where'] as $where){

                        $cleanPlaceholder = str_replace("_", "", $fieldnames[$count]);
                        $cleanPlaceholder = str_replace(".", "", $cleanPlaceholder);
                        $sql.=$fieldnames[$count].$where[0].":".$cleanPlaceholder." AND ";
                        $bindings[$cleanPlaceholder]=$where[1];
                        $count++;
                    }
                    $sql = substr($sql, 0, -5);  //Remove the last AND
                }
                else{ //no where clause so set it to an always true check
                    $sql.='1=1';
                    $bindings=array('1'=>'1'); //Provide default bindings for the statement
                }

                $sql .= ';'; //Add the semi-colon to note the end of the query
                echo $sql . "<br/><br/>";
            //  exit();
                $stmt = $this->_connection->prepare($sql);

                foreach($bindings as $placeholder=>$bound){
                    echo $placeholder . " - " . $bound."<br/>";
                    $stmt->bindParam($placeholder, $bound);
                }

                $result = $stmt->execute();
                echo $stmt->rowCount() . " records<br/>";

                $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

我正在动态构建查询,因此我通过去除句点和下划线来清理占位符 - 因此使用“cleanPlaceholder”变量。

正在生成的查询如下所示:

SELECT * FROM esl_comments WHERE esl_comments.commentVisible=:eslcommentscommentVisible AND esl_comments.commentID=:eslcommentscommentID;

而被绑定的参数是这样的:

eslcommentscommentVisible - Y
eslcommentscommentID - 1

【问题讨论】:

  • 显示查询的最终 SQL 字符串有效和无效。如果没有,也会显示错误消息。

标签: php mysql pdo


【解决方案1】:

bindParam 需要参考

问题是你在foreach循环中绑定参数的方式造成的。

foreach($bindings as $placeholder=>$bound){
    echo $placeholder . " - " . $bound."<br/>";
    $stmt->bindParam($placeholder, $bound);
}

bindParam 需要引用。它将变量而不是值绑定到语句。由于 foreach 循环中的变量在每次迭代开始时都会重置,因此只有对 $bound 的最后一个引用保持不变,并且您最终会将所有占位符绑定到它。

这就是为什么当$query['where'] 仅包含一个条目时您的代码可以工作,但当它包含多个条目时会失败。

您可以通过两种方式解决问题:

通过引用传递

foreach($bindings as $placeholder => &$bound) {  //pass $bound as a reference (&)
    $stmt->bindParam($placeholder, $bound);     // bind the variable to the statement
}

按值传递

使用bindValue 代替bindParam

foreach($bindings as $placeholder => $bound) {  
    $stmt->bindValue($placeholder, $bound);     // bind the value to the statement
}

【讨论】:

  • 太棒了!我没有意识到 bindValue() 和 bindParam() 之间的区别。这为我解决了这个问题。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2013-10-03
  • 2011-01-26
  • 2012-04-20
  • 1970-01-01
  • 2014-04-11
相关资源
最近更新 更多