我的 PHP 函数中有哪些问题导致发生致命错误?
What are the issues in my PHP function that are causing a fatal error to occur?
getUserIDForEmail
应该查询 MySQL 数据库并搜索电子邮件地址并获取相应的用户 ID。
这个测试用例:
$adminId = getUserIDForEmail("a.h@outlook.com");
导致此错误:
Fatal error: Uncaught Error: Call to a member function execute() on boolean in /.../public_html/db.php:51 Stack trace: #0 /.../public_html/register.php(5): getUserIDForEmail('a.h@outlook.com') #1 {main} thrown in /.../public_html/db.php on line 51
第 51 行是:
$stmt = $stmt->execute();
来自这段代码:
/* code omitted here */
$conn = new mysqli(DB['servername'], DB['username'], DB['password'], DB['dbname']);
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
session_start();
/* code omitted here */
/**
* Query database for registered email and get user ID.
*
* @param {String} $email
* @return {Number} $userID
*/
function getUserIDForEmail($email) {
global $conn;
$userID = -1;
$sql = '';
$stmt = null;
$row = null;
if (empty($email)) {
throw new Exception("Email is undefined");
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception("Email is not correctly formatted");
}
$sql = "SELECT `id` FROM `UserAccount` WHERE `email` = ?";
/* Avoid SQL injection */
if ($stmt = $conn->prepare($sql)) {
$stmt = $stmt->bind_param('s', $email);
$stmt = $stmt->execute();
if ($stmt->num_rows > 0) {
$row = $result->fetch_assoc(); // first result
$userID = $row["id"];
}
$stmt->close();
}
return $userID;
}
bind_param()
returns a boolean
并且您正在将其重新分配给 $stmt
。您正在对 execute()
做同样的事情。只需调用 $stmt
对象上的方法:
$stmt->bind_param('s', $email);
$stmt->execute();
那么你会遇到问题,因为没有 $result
:
$row = $result->fetch_assoc();
所以只需 fetch()
来自语句:
$row = $stmt->fetch(PDO::FETCH_ASSOC);
mysqli_stmt::bind_param returns 布尔值:
http://php.net/manual/en/mysqli-stmt.bind-param.php
只需从#50 行删除“$stmt =”即可。
getUserIDForEmail
应该查询 MySQL 数据库并搜索电子邮件地址并获取相应的用户 ID。
这个测试用例:
$adminId = getUserIDForEmail("a.h@outlook.com");
导致此错误:
Fatal error: Uncaught Error: Call to a member function execute() on boolean in /.../public_html/db.php:51 Stack trace: #0 /.../public_html/register.php(5): getUserIDForEmail('a.h@outlook.com') #1 {main} thrown in /.../public_html/db.php on line 51
第 51 行是:
$stmt = $stmt->execute();
来自这段代码:
/* code omitted here */
$conn = new mysqli(DB['servername'], DB['username'], DB['password'], DB['dbname']);
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
session_start();
/* code omitted here */
/**
* Query database for registered email and get user ID.
*
* @param {String} $email
* @return {Number} $userID
*/
function getUserIDForEmail($email) {
global $conn;
$userID = -1;
$sql = '';
$stmt = null;
$row = null;
if (empty($email)) {
throw new Exception("Email is undefined");
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception("Email is not correctly formatted");
}
$sql = "SELECT `id` FROM `UserAccount` WHERE `email` = ?";
/* Avoid SQL injection */
if ($stmt = $conn->prepare($sql)) {
$stmt = $stmt->bind_param('s', $email);
$stmt = $stmt->execute();
if ($stmt->num_rows > 0) {
$row = $result->fetch_assoc(); // first result
$userID = $row["id"];
}
$stmt->close();
}
return $userID;
}
bind_param()
returns a boolean
并且您正在将其重新分配给 $stmt
。您正在对 execute()
做同样的事情。只需调用 $stmt
对象上的方法:
$stmt->bind_param('s', $email);
$stmt->execute();
那么你会遇到问题,因为没有 $result
:
$row = $result->fetch_assoc();
所以只需 fetch()
来自语句:
$row = $stmt->fetch(PDO::FETCH_ASSOC);
mysqli_stmt::bind_param returns 布尔值: http://php.net/manual/en/mysqli-stmt.bind-param.php
只需从#50 行删除“$stmt =”即可。