2012-09-16 142 views
1

我需要幫助理解將在一種方法中創建的對象(MySQL連接)傳遞給同一類中的另一種方法的正確方法。將數據庫對象從一種方法傳遞到另一種方法的正確方法是什麼

我想創建一個數據庫類,使用構造函數和所有其他方法加載連接,使用此連接執行各自的功能。

在嘗試這樣做,我不斷收到此錯誤:

Fatal error: Call to a member function query() on a non-object in /database.php on line 44

我的代碼:

class database 
{ 
    public $mysqli; 

    public function __construct($database_server, $database_name, $database_user, $database_pass) 
    { 
     $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

     if ($mysqli->connect_error) 
     { 
        die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); 
     } 
     echo "Database connection established successfully.<br>"; 
     return $this->mysqli; 

    } 

    public function fetch_data() 
    { 

     $query = "select * from payment"; 
     if ($result = $this->mysqli->query($query)) 
     { 

      // fetch associative array 
      while ($row = $result->fetch_assoc()) 
      { 
       printf ("%s (%s)\n", $row["id"], $row["status"]); 
      } 

     } 

    } 
} 

任何幫助,將不勝感激。謝謝大家。

回答

1
public function __construct($database_server, $database_name, $database_user, $database_pass) 
{ 
    $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

    if ($mysqli->connect_error) 
    { 
       die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); 
    } 
    echo "Database connection established successfully.<br>"; 

    // instead of return, just set the local variable to the `mysqli` property. 
    $this->mysqli = $mysqli; 

} 

或者

public function __construct($database_server, $database_name, $database_user, $database_pass) 
{ 
    $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

    if ($this->mysqli->connect_error) 
    { 
     die('Connect Error (' . $this^>mysqli->connect_errno . ') '. $this->mysqli->connect_error); 
    } 
    echo "Database connection established successfully.<br>";  
} 

而且將更好地爲protected

+0

謝謝您花時間回答mt問題。你的代碼實際上幫助我糾正了我愚蠢的錯誤。另外,如果所有函數都返回一個值。所以我不應該使用 $ this-> mysqli = $ mysqli; return $ this-> mysqli; 這是必需的嗎?這是一種矯枉過正?我想了解正確和正確的方法來做到這一點。 – user1675547

+0

@ user1675547不,構造函數不應返回值。 – xdazz

1

你不是在任何地方設置$this->mysqli,你需要在你的構造中做$this->mysqli = $mysqli;而不是return $this->mysqli

+0

非常感謝你:)這確實解決了我的問題。 – user1675547

0

你實際上並沒有創建$ mysqli作爲你的類的一個屬性。

您需要:

class database 
{ 

public $mysqli; 

public function __construct($database_server, $database_name, $database_user, $database_pass) 
{ 
    $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

    if ($this->mysqli->connect_error) 
    { 
       die('Connect Error (' . $this->mysqli->connect_errno . ') '. $this->mysqli->connect_error); 
    } 
    echo "Database connection established successfully.<br>"; 
    return $this->mysqli; 

} 

class database 
{ 

public $mysqli; 

public function __construct($database_server, $database_name, $database_user, $database_pass) 
{ 
    $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

    if ($mysqli->connect_error) 
    { 
       die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); 
    } 
    echo "Database connection established successfully.<br>"; 
    $this->mysqli = $mysqli; 
    return $this->mysqli; 

} 
+0

謝謝Liam幫助我。我真的很感激。 – user1675547

0

正如其他人已經提到的,你應該這樣設置的mysqli:

$this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

我建議不要在死構造函數,那不是數據庫類決定,而是數據庫類的用戶。改變這樣的:

class database 
{ 
    private $mysqli; 
    public $error = false; 

    public function __construct($database_server, $database_name, $database_user, $database_pass) 
    { 
     $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name); 

     if ($mysqli->connect_error) 
      $this->error = 'Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error); 

    } 

在構建這樣$db = new Database(...);您可以檢查錯誤屬性,然後決定做什麼數據庫。

+0

偉大的輸入。我會記住這一點。謝謝。 – user1675547

相關問題