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
    评论
使用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、付费专栏及课程。

余额充值