0

我正在尝试使用数组查询 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 个结果。任何帮助,将不胜感激!

4

2 回答 2

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/>';
}
于 2011-06-27T20:55:31.047 回答
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!*/ }
于 2011-08-02T18:11:57.807 回答