难以将阵列用作PDO"IN"的一部分.询问 [英] Difficulty using an array as part of PDO "IN" query

查看:99
本文介绍了难以将阵列用作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"的一部分.询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆