2017-03-22 70 views
1

我在PHP下面的代碼連接到我的數據庫:PHP - 呼叫到空的成員函數查詢() - 錯誤

<?php 
class MY_SQL{ 
    private $username; 
    private $password; 
    private $conn; 

    public function __construct($SERVERNAME){ 
     $this->username = "username"; 
     $this->password = "password"; 

     if($SERVERNAME == "data_"){ 
      $server = "Servername"; 
     } 
     else { 
      $server = $SERVERNAME; 
     } 

     // Create connection 
     $conn = new mysqli($server, $this->username, $this->password); 

     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 
    } 

    public function SQLCommand($cmd) { 
     if ($this->conn->query($cmd) === TRUE) { 
      echo "New record created successfully"; 
     } else { 
      echo "Error: " . $cmd . "<br>" . $conn->error; 
     } 
    } 
} 

$sql = "INSERT INTO _test(test1, test2) VALUES ('hello','hi');"; 

     $database = new MY_SQL("Servername"); 
     $database->SQLCommand($sql); 
?> 

我收到以下錯誤:

Fatal error: Call to a member function query() on null

出了什麼問題?

+0

忘記選擇數據庫提高你的類! – Saty

+0

@Saty你是對的! – float

回答

4
$this->conn = $conn; in __construct() 
+0

這樣做的工作!謝謝! – float

3

我建議你用這個例子(從https://github.com/opencart/opencart/blob/master/upload/system/library/db/mysqli.php拍攝)

final class My_SQLi 
{ 
    private $connection; 

    public function __construct($hostname, $username, $password, $database, $port = '3306') 
    { 
     $this->connection = new \mysqli($hostname, $username, $password, $database, $port); 

     if ($this->connection->connect_error) { 
      throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno); 
     } 

     $this->connection->set_charset("utf8"); 
     $this->connection->query("SET SQL_MODE = ''"); 
    } 

    public function query($sql) 
    { 
     $query = $this->connection->query($sql); 

     if (!$this->connection->errno) { 
      if ($query instanceof \mysqli_result) { 
       $data = array(); 

       while ($row = $query->fetch_assoc()) { 
        $data[] = $row; 
       } 

       $result = new \stdClass(); 
       $result->num_rows = $query->num_rows; 
       $result->row = isset($data[0]) ? $data[0] : array(); 
       $result->rows = $data; 

       $query->close(); 

       return $result; 
      } else { 
       return true; 
      } 
     } else { 
      throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno . '<br />' . $sql); 
     } 
    } 

    public function escape($value) 
    { 
     return $this->connection->real_escape_string($value); 
    } 

    public function countAffected() 
    { 
     return $this->connection->affected_rows; 
    } 

    public function getLastId() 
    { 
     return $this->connection->insert_id; 
    } 

    public function isConnected() 
    { 
     return $this->connection->ping(); 
    } 

    public function __destruct() 
    { 
     $this->connection->close(); 
    } 
} 

$sql = "INSERT INTO _test(test1, test2) VALUES ('hello','hi');"; 

$mysql = new My_SQLi('host', 'user', 'password', 'db'); 
$result = $mysql->query($sql); 
+0

謝謝我會看看! – float

相關問題