如何使用IN与pdo查询多个选择

时间:2014-01-07 03:45:57

标签: php pdo

你将如何使用pdo

        $post = Array
 (
 [0] => 558
[1] => 494
[2] => 469
[3] => 459
[4] => 452
[5] => 451
 )
   $ids =  implode(',' post);
   mysql_query('Select *  where post IN ('$ids'));

目前我在做

$query = "Select * where post in (:ids);";
$stmt = $pdo=>prepare($query);
$stmt =>execute(array('ids'=> $ids);

但这似乎没有太多工作

2 个答案:

答案 0 :(得分:0)

您的代码似乎无效。请尝试这样:

$array=array("558","494","469","459","452","451");
$in_list = "'".implode("','",$array)."'";
$stmt = $this->db->prepare('Select * FROM table where post IN ('.$in_list.')');
$stmt->execute();

有关准备声明的详细信息,请阅读here

答案 1 :(得分:0)

您可以动态构建占位符?

$post = Array(....);
//count $post size, repeat a string of '?,'
$in  = str_repeat('?,', count($post) - 1) . '?';

$query = "SELECT * FROM tbl WHERE post IN ($in);";
$stmt = $pdo=>prepare($query);
$stmt =>execute($post);