2015-02-07 142 views
-1

好吧,我正在学习MySQLi,我很难让登录脚本表现得很好。它给我一个对非成员函数query()的非对象错误的调用。从我所读到的,这可能是因为我没有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 
?> 
+0

你能突出显示错误发生的位置吗? – 2015-02-07 17:39:18

+0

什么是'mysqli :: conn'应该是? (请参阅[文档](http://php.net/manual/en/book.mysqli.php)) – Sumurai8 2015-02-07 17:39:40

+0

错误发生在第14行,其中$ result开始 – 2015-02-07 17:41:51

回答

1

Call to a member function query() on a non-object意味着你正在尝试调用的东西的功能query()不是一个对象。您正在拨打$db->conn->query($sql),因此$db->conn不是一个对象。 $db是一个Mysqli对象。 Reading the documentation得知我们没有MySQLi::conn这样的东西。

我想你打电话给MySQLi::query(...)docs)。将代码更改为$db->query($sql)并且问题消失。

相关问题