嗨,我想在每次循环问题中使用from子句sql查询中的不同表是不接受我的代码吗?这是代码
$tbl0="atom_supplier_oe";
$tbl1="atom_supplier_silver_coated_slotted_and_crossed_drilled";
$tbl2="atom_supplier_black_coated_slotted_and_crossed_drilled";
$tbl3="atom_supplier_just_crossed_drilled";
$tbl4="atom_supplier_just_slotted";
$tbl5="atom_supplier_slotted_and_crossed_drilled";
for($i=0;$i<6;$i++){
$sql="select * from $tbl'+$i' where client_id='$id'";
if(!$result=mysqli_query($db,$sql)){
echo mysql_error($db);
}
$count=mysqli_num_rows($result);
if($count==0){
break;
//echo mysqli_error($db);
}
有什么问题?我想为每次迭代使用不同的表进行比较。
尝试使用类似这样的东西。数组中的表名,遍历该数组并相应地处理结果。
$id = ''; // not sure where you are getting this from
$db = ''; // again this was not in your example above
$tables = [
"atom_supplier_oe",
"atom_supplier_silver_coated_slotted_and_crossed_drilled",
"atom_supplier_black_coated_slotted_and_crossed_drilled",
"atom_supplier_just_crossed_drilled",
"atom_supplier_just_slotted",
"atom_supplier_slotted_and_crossed_drilled",
];
foreach ($tables as $table) {
$sql = sprintf("SELECT * FROM `%s` WHERE client_id = %d", $table, $id);
if (!$result=mysqli_query($db,$sql)) {
echo mysql_error($db);
continue;
// or break if you want to still check the other tables.
}
// do something with the result here ($result)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句