Php 对非对象MySQLi调用成员函数query()

Php 对非对象MySQLi调用成员函数query(),php,mysql,mysqli,Php,Mysql,Mysqli,好的,我正在学习MySQLi,我很难让我的登录脚本正常工作。它在一个非对象错误上调用成员函数query()。从我所读到的内容来看,这可能是因为我的范围中没有MySQLi。我看不出错误在哪里,如果有人能指出并向我解释这个问题,我会很高兴的 <?php session_start(); // Start PHP // Get info sent to server from login form. $my_username = $_POST['username']; $my_password

好的,我正在学习MySQLi,我很难让我的登录脚本正常工作。它在一个非对象错误上调用成员函数query()。从我所读到的内容来看,这可能是因为我的范围中没有MySQLi。我看不出错误在哪里,如果有人能指出并向我解释这个问题,我会很高兴的

<?php session_start(); // Start PHP
// Get info sent to server from login form.
$my_username = $_POST['username'];
$my_password = $_POST['password'];
// MD5 Encrypt the password.
$my_password_md5 = md5($my_password);
// Define MySQL Information.
$db = new MySQLi('localhost', 'DBUSERNAME', 'DBPASS!', 'DB');
if ($db->connect_error) {
$error = $db->connect_error;
}
// Check table for username provided.
$sql="SELECT * FROM TABLE WHERE username='$my_username' and password='$my_password_md5'".
//this is where the error occurs
$result=$db->conn->query($sql) or die("Error in the consult.." . mysqli_error($db));
$rows=mysqli_fetch_assoc($result);
// Count how many rows match that information.
$count=mysqli_num_rows($result);
// Check if there are any matches.
if($count==1){
// If so, register $my_username, $my_password and redirect to the index page.
ini_set("session.gc_maxlifetime", "18000"); 
session_cache_expire(18000);
$cache_expire = session_cache_expire();
$_SESSION['username'] = $my_username;
$_SESSION['id'] = $rows['id'];
header("location:WEBSITE");
}

// If not, redirect back to the index page and provide an error.
else {
header("location:WEBSITE");
}
// End PHP
?>

调用非对象上的成员函数query()
表示您试图调用非对象上的函数
query()
。您正在调用
$db->conn->query($sql)
,因此
$db->conn
不是对象
$db
是一个Mysqli对象。揭示了没有像
MySQLi::conn
这样的东西


我假设您想调用
MySQLi::query(…)
()。将代码更改为
$db->query($sql)
您的问题就会消失。

您可以突出显示错误发生的位置吗?mysqli::conn应该是什么?(请参阅)错误发生在第14行$result开始的地方,@Fred ii-所说的是真的:D,因为$conn没有在那里声明,而是$db,对吗?可能是