2015-05-04 66 views
0

我一直在嘗試一切超過4小時,並將錯誤固定到此代碼段。你能告訴我這裏可能有什麼錯嗎?Php Mysqli內部錯誤

<?php 
include_once 'database.php'; 

class Model { 

    public $db; 
    public $data; 
    public $data_item = array(); 


    public function output(){ 
     $this->db = new mysqli($db_servername, $db_username, $db_password, $db_name, $db_port); 
     $this->data = $this->db->query('SELECT * FROM `tbl_restaurants`'); 
     while($row = $this->data->fetch_assoc()){ 

      $this->data_item['res_id'] = $row['res_id']; 

     } 

     return $this->data_item['res_id']; 
    } 

} 
$obj = new Model; 
echo $obj->output(); 
?> 
+0

'tbl_restaurants'刪除。引用和嘗試。不要刪除第一個和最後一個報價 –

+0

@anantkumarsingh它們不是引號。 –

+1

從哪裏獲得變量'$ db_servername' ...?可能是這些變量的範圍是原因。 –

回答

0

我認爲$db_servernamedatabase.php中

需要global此:

public function output(){ 
    global $db_servername, $db_username, $db_password, $db_name, $db_port; 
    $this->db = new mysqli($db_servername, $db_username, $db_password, $db_name, $db_port); 
    //... 
} 

一個更好的設計是,給數據庫連接的參數到型號:

public function __construct($db) { 
    $this->db = $db; 
} 
+1

OMG!謝謝你,兄弟! –

0

您假定數據庫連接的詳細信息,但沒有從任何地方獲取。

數據庫未連接。

$this->db = new mysqli($db_servername, $db_username, $db_password, $db_name, $db_port); 

通過它明確爲:

public function output($db_servername, $db_username, $db_password, $db_name, $db_port){ 

或將它們設置爲類變量,並設置它通過構造函數:

類模型{

public $db; 
public $data; 
public $data_item = array(); 
public $db_servername; 
public $db_username; 
public $db_password; 
public $db_name; 
public $db_port; 

,並讓他們爲:

public function output(){ 
    $this->db = new mysqli($this->db_servername, $this->db_username, $this->db_password, $this->db_name, $db_port); 
+0

你能進一步解釋一下嗎? *編輯:剛剛嘗試了你說的,仍然沒有工作。 –

0

$ db_servername,$ db_username,$ db_password,$ db_name,$ db_port未在類中定義。試試這個:

<?php 
include_once 'database.php'; 

class Model { 

    public $db; 
    public $data; 
    public $data_item = array(); 


    public function output(){ 
     global $db_servername, $db_username, $db_password, $db_name, $db_port; 
     $this->db = new mysqli($db_servername, $db_username, $db_password, $db_name, $db_port); 
     $this->data = $this->db->query('SELECT * FROM `tbl_restaurants`'); 
     while($row = $this->data->fetch_assoc()){ 

      $this->data_item['res_id'] = $row['res_id']; 

     } 

     return $this->data_item['res_id']; 
    } 

} 
$obj = new Model; 
echo $obj->output(); 
0

假設數據庫詳細信息是問題,您可以將它們全局化,也可以不使用變量,而是使用database.php中的定義

而不是$ db_servername =「localhost」;等等。你可以這樣做:

define('DB_SERVERNAME','localhost'); 

然後,而不是在新的mysqli使用$ db_servername(功能,你會怎麼做:

$this->db = new mysqli(DB_SERVERNAME, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT); 
0

試試這個

<?php 
include_once 'database.php'; 

class Model { 

    public $db; 
    public $data; 
    public $data_item = array(); 


    public function output($db_servername, $db_username, $db_password, $db_name, $db_port){ 
     $this->db = new mysqli($db_servername, $db_username, $db_password, $db_name, $db_port); 
     $this->data = $this->db->query('SELECT * FROM `tbl_restaurants`'); 
     while($row = $this->data->fetch_assoc()){ 

      $this->data_item['res_id'] = $row['res_id']; 

     } 

     return $this->data_item['res_id']; 
    } 

} 
$obj = new Model; 
echo $obj->output($db_servername, $db_username, $db_password, $db_name, $db_port); 
?>