2015-02-07 136 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)並且問題消失。

相關問題