問題描述
以下代碼拋出了神秘的警告.我無法理解他們的意思.這些錯誤表明什么以及如何消除它們?
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.
外賣:解決您的問題
- 按照我的文章conn.php> 設置正確的連接設置
將你的程序 mysqli 重寫為對象語法,例如
- 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();
獲取真正的錯誤信息
在那之后,您將能夠解決問題.
and after that you'll be able to fix the issue.
這篇關于警告 :: 無效的對象或資源 mysqli_stmt.含義和解決方案是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!