Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement error

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

I had a mysql query and I was converting it to mysqli(prepared statement) but I ran in to a problem which throws the below error,

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement 

Mysql code

$random_name_generated = vpb_generate_random_name().'.jpg'; //Generated name for uploaded files or images                      if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location))                      {                     $check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."'  and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");                     if(mysql_num_rows($check_empty_field) 

The below is what I tried even though it didn't work. I already knew it won't work but I wanted to try it myself before asking a question. And the error is coming from the $get_empty_field = $stmt->bind_result($stmt); Can anyone help me figure out what went wrong?

if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location))                  {                 $firstname = '""';                 $lastname = '""';                 $stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ?  and `firstname` = ? and `lastname` = ?");                 $stmt->bind_param('sss', $username, $firstname, $lastname);                 $stmt->execute();                 $stmt->store_result();                 if ($stmt->num_rows prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)");                 $stmt->bind_param('sssssssss',  $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date);                 $stmt->execute();                 $stmt->close();                    $identity = "image_one";                 }                 else                 {                 $get_empty_field = $stmt->bind_result($stmt);                 $image_one = strip_tags($get_empty_field["image_one"]);                 $image_two = strip_tags($get_empty_field["image_two"]);                 $image_three = strip_tags($get_empty_field["image_three"]);                 $image_four = strip_tags($get_empty_field["image_four"]);                 $image_five = strip_tags($get_empty_field["image_five"]);                 global $identity; 

回答1:

You need to change

$get_empty_field = $stmt->bind_result($stmt); 

To

$get_empty_field = $stmt->bind_result($field1, $field2, $field3); 

The number of $fieldx variables being equal to the number of fields that are selected. If you don't know how many there are, use this:

  // Throw an exception if the result metadata cannot be retrieved   if (!$meta = $stmt->result_metadata())   {     throw new Exception($stmt->error);   }    // The data array   $data = array();    // The references array   $refs = array();    // Iterate over the fields and set a reference   while ($name = $meta->fetch_field())   {     $refs[] =& $data[$name->name];   }    // Free the metadata result   $meta->free_result();    // Throw an exception if the result cannot be bound   if (!call_user_func_array(array($stmt, 'bind_result'), $refs))   {     throw new Exception($stmt->error);   } 

And then you access the result, after fetching, with $data['field'];



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!