mysql_stmt含义,警告::无效的对象或资源mysqli_stmt。含义和解决方案是什么?

The following code is throwing the mysterious Warnings. I can't understand what they mean. What do these errors indicate and how to eradicate them?

require "conn.php";

$q = mysqli_stmt_init($dbconn);

$query = "SELECT users.userid FROM users WHERE users.email = ? ";

mysqli_stmt_prepare($q, $query);

mysqli_stmt_bind_param($q, "s", $email);

mysqli_stmt_execute($q);

$result = mysqli_stmt_get_result($q);

if (mysqli_num_rows($result) == 0) {

$q = mysqli_stmt_init($dbconn);

$query = "INSERT INTO users ( users.first_name, users.last_name, users.mobile_no, users.email, users.password, users.reg_date)

VALUES (? ,? ,? ,? ,? ,NOW() )";

mysqli_stmt_prepare($q, $query);

mysqli_stmt_bind_param($q, "sssss", $first_name, $last_name, $mobile_number, $email, $password);

mysqli_stmt_execute($q);

if (mysqli_stmt_affected_rows($q) == 1) {

echo "data inserted
";

foreach ($_POST as $key => $val) {

echo "$key - - - > $val
";

}

}

} else {

echo "email is already registered";

}

whenever I run this block of code following warnings occur

Warning: mysqli_stmt_bind_param(): invalid object or resource mysqli_stmt in /storage/emulated/0/htdocs/registration_process.php on line 66

Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt in /storage/emulated/0/htdocs/registration_process.php on line 67

Warning: mysqli_stmt_get_result(): invalid object or resource mysqli_stmt in /storage/emulated/0/htdocs/registration_process.php on line 68

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /storage/emulated/0/htdocs/registration_process.php on line 70

解决方案

The problem here is quite peculiar. It sounds unrelated but this error message is the result of unreliable syntax variant.

Procedural mysqli syntax is not only excessively verbose as compared to object syntax, but also deceptive, raising an error not when it really occurs, but when it's already too late, not to mention the cryptic nature of this error message.

There is some problem with your query and you need to get the real error message from MySQL as explained in this answer but in order to implement it you have to change the syntax.

Takeaway: to solve your problem

rewrite your conn.php as shown in this my article to set the proper connection settings

rewrite your procedural mysqli to object syntax, such as

$query = "SELECT userid FROM users WHERE email = ?";

$stmt = $dbconn->prepare($query);

$stmt->bind_param("s", $email);

$stmt->execute();

$result = $stmt->get_result();

get the real error message

if the error is already clear then just fix it, or otherwise google for the better explanation

and after that you'll be able to fix the issue.

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值