2016-08-19 137 views
-4

所示的錯誤是PHP登錄和註冊錯誤

"Fatal error: Uncaught Error: Call to a member function query() on string in D:\Users\kukor\Documents\XAMPP\htdocs\timetable\class.ManageUsers.php:22 Stack trace: #0 D:\Users\kukor\Documents\XAMPP\htdocs\timetable\login.php(19): ManageUsers->LoginUsers('kayzmark', 'ultimate') #1 {main} thrown in D:\Users\kukor\Documents\XAMPP\htdocs\timetable\class.ManageUsers.php on line 22"

<?php 
class ManageUsers{ 
    public $link; 

    function __construct(){ 
     $db_connection = new dbConnection(); 
     $this->link = $db_connection->connect(); 
     return $this->link; 
    } 

    function registerUsers($password, $ip_address, $date, $time, $username, $email, $uname){ 
     $query = $this->link->prepare("INSERT INTO users (password,ip_address,date,time,username, email, uname) VALUES(?,?,?,?,?,?,?)"); 
     $values = array ($password, $ip_address, $date, $time, $username, $email, $uname); 
     $query->execute($values); 
     $count = $query->rowCount(); 
     return $count; 
    } 

    function LoginUsers($username, $password){ 
     $query = $this->link->query("SELECT * FROM users WHERE username='$username' AND password='$password'"); 
     $rowCount = $query->rowCount(); 
     return $rowCount; 
    } 

    function GetUserInfo($username){ 
     $query = $this->link->query("SELECT * FROM users WHERE username = '$username'"); 
     $rowCount = $query->rowCount(); 
     if($rowCount ==1) 
     { 
      $result = $query->fetchAll(); 
      return $result; 
     } 
     else 
     { 
      return $rowCount; 
     } 
    } 
} 
?> 
+0

Hi @KayzMark,歡迎來到StackOverflow。請確保包含一個明確的__problem聲明___以確定您在將來發布問題時究竟需要什麼幫助。請確保您查看[幫助中心](http://stackoverflow.com/help)以獲取[mcve](http://stackoverflow.com/help/mcve)和[我應該避免哪些類型的問題問](http://stackoverflow.com/help/dont-ask)供將來參考。您提供的錯誤消息非常簡單。您在該文件的_line 22_上調用了一個屬於「String」而不是「Object」的方法。 – Sherif

回答

0

您probaly呼籲​​一個字符串,而不是對象。你能否驗證你調用query()的變量是否包含正確的對象,而不是錯誤地重寫它?

+0

覆蓋它? – KayzMark

+0

錯誤$ this - > link ='abc'以及後面的代碼$ this - > link - > query() – Bolivir

+0

Hi @Bolivir,這個答案應該是一個評論,因爲需要進一步的討論來理解這個問題。請記住,StackOverflow不是獲得調試幫助的地方。請務必在幫助中心查看[我如何寫出一個好答案](http://stackoverflow.com/help/how-to-answer),以備將來參考。謝謝! – Sherif

0

@Bolivir是正確的:你得到的錯誤是因爲$this-link是一個字符串,而不是一個對象。

難道$this->link = $db_connection->connect();也可以返回一個字符串而不是連接(例如,當您輸入錯誤憑證或數據庫關閉時)?

或者不應該$this->link請參考$db_connection?只是在這裏嘗試一些東西,因爲我不知道你使用的是什麼數據包封裝。

dbConnection::connect() -method是什麼樣的?

+0

'公共功能連接(){ \t \t \t嘗試{ \t \t \t \t $這個 - > db_conn =新PDO( 「mysql的:主機= $這個 - > DB_HOST; DBNAME = $這個 - > DB_NAME」,這$ - > DB_USER,$這個 - > DB_PASS); \t \t \t \t return $ this-> db_conn; \t \t \t} \t \t \t趕上(PDOException $ E) \t \t \t { \t \t \t \t返回$ E->的getMessage(); \t \t \t} \t \t}' – KayzMark

+0

在你的功能來看我的猜測是,你沒有得到一個'PDO'對象返回,但錯誤信息'$ E->的getMessage()'。閱讀你的消息(在你的案例中'echo $ this-> link'),你知道什麼是錯的。但更好的設計是,如果你的'dbConnection'類拋出一個錯誤,你的'ManageUsers'類捕獲它。 –

+0

所以這是更好的權利'公共功能dbConnection() \t { \t $ this-> conn = null; 嘗試 \t \t {this-> conn = new PDO(「mysql:host =」。$ this-> host。「; dbname =」。$ this-> db_name,$ this-> username,$ this- >密碼); \t \t \t $ this-> conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); } \t \t趕上(PDOException $除外) \t \t { 回聲 「連接錯誤」。 $ exception->的getMessage(); } return $ this-> conn; }' – KayzMark