我正在尝试使用数组查询MySQL数据库。
$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个结果。任何帮助将不胜感激!
答案 0 :(得分:1)
您可以尝试这种方式 - 未经测试 - :
// To fetch your array data
$array=array('Group1','Group2','Group3');
try {
$STH = $this->DBH->prepare("SELECT GroupName FROM myTable WHERE GroupName IN (?)");
$STH->execute($array);
while($lines=$STH->fetch($this->DBH->FETCH_OBJ))
{
echo $lines->GroupName.'<br />';
}
}
catch(PDOException $e) {
/*Panic!*/
echo 'ERR: ' .$e->getMessage().'<br/>';
}
答案 1 :(得分:0)
以防其他任何人遇到过这个......
这似乎是一个逃避内爆阵列的问题。
$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!*/ }