本文介绍了难以将阵列用作PDO"IN"的一部分.询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用数组查询MySQL数据库.
I'm trying to query a MySQL databse using an array.
$array=array('Group1','Group2','Group3');
$inQuery=implode(",",$array);
//$inQuery='Group1'; //This returns the expected result, but is obviously not an array
$data=array($inQuery);
try {
$STH = $this->DBH->prepare('SELECT GroupName FROM myTable WHERE GroupName IN(?)');
$STH->execute($data);
/* Output results*/
}
catch(PDOException $e) { /*Panic!*/ }
我没有收到任何错误消息,只有0条结果.任何帮助将不胜感激!
I am not getting any error messages, just 0 results. Any help would be appreciated!
推荐答案
以防万一有人碰到这个问题.
Just in case anyone else ever comes across this....
转义内陷数组似乎是个问题.
It appeared to be an issue with escaping the imploded array.
$array=array('Group1','Group2','Group3');
$inQuery=implode(",",$array);
$inQuery="'".$inQuery."'"; //Solved the issue.
$data=array();
try {
$STH = $this->DBH->prepare('SELECT GroupName FROM myTable WHERE GroupName IN($inQuery)');
$STH->execute($data);
/* Output results*/
}
catch(PDOException $e) { /*Panic!*/ }
这篇关于难以将阵列用作PDO"IN"的一部分.询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!