如何在php中的prepare语句中绑定参数?

时间:2012-09-29 13:35:47

标签: php mysqli prepared-statement sqlbindparameter

我想在prepare语句中发送数组值,但它不起作用

我的代码是:

for($bb=0; $bb<sizeof($subject_temp_array)-1; $bb++)
 {
    $sql ="select * from tbl_subjects where sub_id = ?";
    if ($result = $mysqli->prepare($sql)) 
    {
       $subject_id = $subject_temp_array[$bb];
       $result -> bind_param("i",$subject_id); 
       $result->execute();
       $result->store_result();
       $rows1 = $result->num_rows;
     }
  }

1 个答案:

答案 0 :(得分:-1)

为什么不使用foreach?此外,您是否遇到任何错误消息?试试这段代码:

error_reporting(-1);
ini_set('display_errors', 1);

foreach($subject_temp_array as $subject_id)
{
    $sql = "select * from tbl_subjects where sub_id = ?";
    if ($result = $mysqli->prepare($sql)) 
    {
       $result->bind_param("i", $subject_id); 
       $result->execute();
       $result->store_result();
       $rows1 = $result->num_rows;
     } else {
         throw new Exception("Error preparing query.");
     }
}