mysql bind result,警告:mysqli_stmt :: bind_result():绑定变量的数量与准备好的语句错误中的字段数量不匹配...

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) < 1)

{

mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')");

$identity = "image_one";

}

else

{

$get_empty_field = mysql_fetch_array($check_empty_field);

$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;

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 < 1)

{

$date = 'date("d-m-Y")';

$image_2 = "''";

$image_3 = "''";

$image_4 = "''";

$image_5 = "''";

$stmt = $mysqli->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;

解决方案

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'];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用mysqli_stmt类处理SELECT查询结果需要经过以下几个步骤: 1. 准备预处理语句:使用mysqli_prepare()函数准备预处理语句。 2. 绑定参数(如果有):如果预处理语句有占位符,需要使用mysqli_stmt_bind_param()函数将参数绑定到占位符上。 3. 执行预处理语句:使用mysqli_stmt_execute()函数执行预处理语句。 4. 获取结果集:使用mysqli_stmt_get_result()函数获取结果集。 5. 遍历结果集:使用mysqli_fetch_array()、mysqli_fetch_assoc()等函数遍历结果集,获取每一行数据。 下面是一个示例代码: ``` // 连接数据库 $mysqli = new mysqli("localhost", "user", "password", "database"); // 准备预处理语句 $stmt = mysqli_prepare($mysqli, "SELECT * FROM users WHERE id > ?"); // 绑定参数 $id = 2; mysqli_stmt_bind_param($stmt, "i", $id); // 执行预处理语句 mysqli_stmt_execute($stmt); // 获取结果集 $result = mysqli_stmt_get_result($stmt); // 遍历结果集 while ($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"] . ", name: " . $row["name"] . "<br>"; } // 关闭预处理语句和数据库连接 mysqli_stmt_close($stmt); mysqli_close($mysqli); ``` 在该示例,我们使用mysqli_prepare()函数准备了一条预处理语句,该语句查询了id大于2的用户信息。接着,我们使用mysqli_stmt_bind_param()函数将参数2绑定到了占位符上。然后,使用mysqli_stmt_execute()函数执行了预处理语句,并使用mysqli_stmt_get_result()函数获取了结果集。最后,我们使用mysqli_fetch_assoc()函数遍历了结果集,将每一行数据输出到了页面上。最后,我们关闭了预处理语句和数据库连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值