警告::无效的对象或资源mysqli_stmt.含义和解决方案是什么? [英] warning :: invalid object or resource mysqli_stmt. What is the meaning and solution(s)?

查看:313
本文介绍了警告::无效的对象或资源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 <br>";
        foreach ($_POST as $key => $val) {
            echo "$key - - - > $val <br>";
        }
    }
} 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.

与对象语法相比,过程mysqli语法不仅过于冗长,而且具有欺骗性,不会在实际发生时发出错误,而是在为时已晚的情况下引发错误. ,更不用说此错误消息的秘密性质.

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.

您的查询存在一些问题,您需要从MySQL获取真实错误消息 ,如该 answer ,但是要实现它,您必须更改语法.

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.

外卖:解决您的问题

  1. 按照我的文章所示的方法重写您的conn.php,以设置正确的连接设置
  2. 将过程mysqli重写为对象语法,例如

  1. rewrite your conn.php as shown in this my article to set the proper connection settings
  2. 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();

  • 获取真实的错误消息

    然后您就可以解决该问题.

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

    这篇关于警告::无效的对象或资源mysqli_stmt.含义和解决方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆