我的代码中有一些混乱,请查看我的代码并建议我如何使用In运算符在查询中传递另一个参数。
$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);
}
我无法将我的猫咪绑定为像preapred一样。请帮帮我谢谢
答案 0 :(得分:2)
你的常识的回答略有不同,而我没有意识到你可以做的事情(尽管从某种意义上讲是有道理的)......
$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);
}
唯一真正的不同是对bind_param
的调用,而不是将项合并到数组中,只是在使用PHP 5.6 +中的数组填充(...)之前将其列为另一个参数。
<强>更新强> 从评论来看,PHP的版本不支持splat :( ...所以回到原来......
$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);
}
这会将类别添加到项目列表中,但请注意array_fill()
使用count-1作为?因为猫已经在那里了。
答案 1 :(得分:0)
没有经过测试,但也许你可以稍微捏一下这个
$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 );
}
答案 2 :(得分:0)
Thanks to splat operator以及其他一些技巧,代码可以合理地缩短为少数几行:
$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);
}
虽然Nigel Ren提供了一个真正干净的解决方案,但是为了保持一个array_merge()调用,我会坚持使用它,因为它更普遍,允许其他占位符不仅在前面而且还在IN()语句之后。这些也可以合并到最终的$ params数组中