问题
I have some confusion in my code please view my code and suggest me how to pass another parameter in query using with In operator.
$cat=1;
$lastnames = $ids;
$arParams = array();
foreach($lastnames as $key => $value)
$arParams[] = &$lastnames[$key];
;
$count_params = count($arParams);
$int = str_repeat('i',$count_params);
array_unshift($arParams,$int);
$q = array_fill(0,$count_params,'?');
$params = implode(',',$q);
$qry1=$dblink->prepare("SELECT * FROM course_details WHERE category=$cat and cat_id IN ($params)");
call_user_func_array(array($qry1, 'bind_param'), $arParams);
$qry1->execute();
$qry1_res=$qry1->get_result();
while($rowset1=$qry1_res->fetch_array()){
print_r($rowset1);
}
I can't bind my cat id as like preapred. please help me Thank you
回答1:
A slight variation on Your Common Sense's answer, and something I didn't realise you could do (although it makes sense in a way)...
$cat=1;
$lastNames = $ids;
$count_params = count($lastNames);
$int = str_repeat('i',$count_params+1);
$q = array_fill(0,$count_params,'?');
$params = implode(',',$q);
$qry1=$dblink->prepare("SELECT * FROM course_details WHERE category=? and cat_id IN ( $params )");
$qry1->bind_param( $int, $cat, ...$lastNames);
$qry1->execute();
$qry1_res=$qry1->get_result();
while($rowset1=$qry1_res->fetch_array()){
print_r($rowset1);
}
The only real different is the call to bind_param
, rather than merging the item into the array, just list it as another parameter before using the array fill (...) from PHP 5.6+.
Update: From the comment, the version of PHP doesn't support splat :(... so going back to original...
$cat=1;
$lastnames = $ids;
$arParams = array();
array_unshift($lastnames,$cat);
foreach($lastnames as $key => $value) {
$arParams[] = &$lastnames[$key];
}
$count_params = count($arParams);
$int = str_repeat('i',$count_params);
array_unshift($arParams,$int);
$q = array_fill(0,$count_params-1,'?');
$params = implode(',',$q);
$qry1=$dblink->prepare("SELECT * FROM course_details WHERE category=? and cat_id IN ( $params )");
call_user_func_array(array($qry1, 'bind_param'), $arParams);
$qry1->execute();
$qry1_res=$qry1->get_result();
while($rowset1=$qry1_res->fetch_array()){
print_r($rowset1);
}
This adds the category into the list of items, but note the array_fill()
uses count-1 as the ? for the cat is already there.
回答2:
Not tested but perhaps you could fudge it a little like this
$cat=1;
$lastnames = $ids;
$arParams = array();
foreach( $lastnames as $key => $value ) $arParams[ $key ] = &$lastnames[ $key ];
$count_params = count( $arParams );
$int = str_repeat( 'i', $count_params + 1 ); /* add one more type string */
array_unshift( $arParams, $int );
$q = array_fill(0,$count_params,'?'); /* add ? for each param */
$params = implode(',', $q );
/* add $cat as first parameter */
array_unshift( $arParams, $cat );
$qry1=$dblink->prepare("SELECT * FROM course_details WHERE category=? and cat_id IN ( $params )");
call_user_func_array( array( $qry1, 'bind_param' ), &$arParams );
$qry1->execute();
$qry1_res=$qry1->get_result();
while( $rowset1=$qry1_res->fetch_array() ){
print_r( $rowset1 );
}
回答3:
Thanks to splat operator and some other tricks the code could be sensibly shortened to a handful of rows:
$qmarks = str_repeat('?,', count($ids) - 1) . '?';
$types = str_repeat('i', count($ids) + 1);
$params = array_merge(array($cat), $ids);
$sql = "SELECT * FROM course_details WHERE category=? and cat_id IN ($qmarks)";
$stmt = $dblink->prepare($sql);
$stmt->bind_param($types, ...$params);
$data = $stmt->get_result()->fetch_all(MYSQL_ASSOC);
foreach ($data as $row){
print_r($row);
}
although Nigel Ren offered a real neat solution, sparing an array_merge() call, I would stick with mine for it being more universal, allowing other placeholders not only in front but also past the IN() statement. Those could be merged into the final $params array as well
来源:https://stackoverflow.com/questions/49106012/mysqli-prepared-statements-with-in-operator-and-one-more-placeholder