简体   繁体   中英

how to query multiple select using IN with pdo

how would you do this using 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'));

currently i'm doing

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

but this does not seem too work

Your code seem to be invalid.Try like this :

$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();

For detail about prepare statement you can read here .

You can dynamically build your placeholders ?

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM