php - array is converted as string in PDO -
below query:
$key = array(1,2); $in = join(',', array_fill(0, count($key), '?')); $statement = $pdo->prepare("select * posts posts.subid in (".$in.") , posts.pricing=? , posts.poscode=? order posts.poscode desc limit 60"); $result = array_merge($key, array($rate,$postcode)); $statement->execute($result);
when replace $key = array(1,2);
$key = array($key);
query fetches data first id whereby assume converts array string.
$key
holds value 1,2 in array shown below:
$a=$data['sub']; $key0=array(); foreach($a $v=>$k) { $key0[]=$v; } $key2=implode(',',$key0); $key = array($key2);
how make pdo understand $key
holds array value , not string?
i solve problem using named placeholders. don't ?
stuff. need placeholder every value of in.
see sample code:
$key = array(1,2); $pricing = "somepricing"; $postcode = "somepostcode"; $bindings = array(); $bindings[] = array(":pricing", $pricing, pdo::param_str); $bindings[] = array(":postcode", $postcode, pdo::param_str); $key_placeholders = array(); foreach($key $k => $v) { $placeholder = ":subid".$k; $bindings[] = array($placeholder, $v, pdo::param_int); $key_placeholders[] = $placeholder; } $sql = "select * posts " . "where posts.subid in (". implode(",",$key_placeholders).") " . "and posts.pricing=:pricing " . "and posts.poscode=:postcode " . "order posts.poscode " . "desc limit 60"; $statement = $pdo->prepare($sql); foreach($bindings $b) { $statement->bindvalue($b[0],$b[1],$b[2]); } $statement->execute();
Comments
Post a Comment