Fatal error: "Call to a member function prepare() on a non-object"
Fatal error: "Call to a member function prepare() on a non-object"
当我 运行 登录时出现此错误:
Fatal error: Call to a member function prepare() on a non-object in /home/lankaf5/public_html/admin/admin/functions.php on line 30
我已经标记了它所指的第 30 行;有人可以检查并告诉我出了什么问题吗?我试了所有可能的方法,还是想不通。
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
function login($email, $password, $conn) {;
$sql ="SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1";
// Using prepared statements means that SQL injection is not possible.
if ($stmt = $conn->prepare($sql)) { // line 30 that the error is referring to
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($user_id, $username, $db_password, $salt);
$stmt->fetch();
// hash the password with the unique salt.
$password = hash('sha512', $password . $salt);
if ($stmt->num_rows == 1) {
// If the user exists we check if the account is locked
// from too many login attempts
if (checkbrute($user_id, $conn) == true) {
// Account is locked
// Send an email to user saying their account is locked
return false;
} else {
首先进行此修复:
function login($email, $password, $conn) {; // <-- remove the semi-colon
现在添加此调整:
function login($email, $password, mysqli $conn) {
这将强制连接成为 mysqli
class,并且在未设置的情况下,它会抛出更接近问题根源的致命错误。
您仍然需要找出为什么这是失败的。我怀疑您的数据库凭据不正确,或者您在调用此函数时可能根本没有设置连接?
当我 运行 登录时出现此错误:
Fatal error: Call to a member function prepare() on a non-object in /home/lankaf5/public_html/admin/admin/functions.php on line 30
我已经标记了它所指的第 30 行;有人可以检查并告诉我出了什么问题吗?我试了所有可能的方法,还是想不通。
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
function login($email, $password, $conn) {;
$sql ="SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1";
// Using prepared statements means that SQL injection is not possible.
if ($stmt = $conn->prepare($sql)) { // line 30 that the error is referring to
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($user_id, $username, $db_password, $salt);
$stmt->fetch();
// hash the password with the unique salt.
$password = hash('sha512', $password . $salt);
if ($stmt->num_rows == 1) {
// If the user exists we check if the account is locked
// from too many login attempts
if (checkbrute($user_id, $conn) == true) {
// Account is locked
// Send an email to user saying their account is locked
return false;
} else {
首先进行此修复:
function login($email, $password, $conn) {; // <-- remove the semi-colon
现在添加此调整:
function login($email, $password, mysqli $conn) {
这将强制连接成为 mysqli
class,并且在未设置的情况下,它会抛出更接近问题根源的致命错误。
您仍然需要找出为什么这是失败的。我怀疑您的数据库凭据不正确,或者您在调用此函数时可能根本没有设置连接?