2014-09-20 110 views
1

從我在線找到的教程構建。簡單mySQLi選擇陣列

我嘗試從'項目'表中選擇所有項目並創建一個數組。不知道這是如何工作的。 $result = $this->connection->query($q);是導致此問題的原因。

<?php 
//DB.class.php 

class DB { 

    protected $db_name = 'dbname'; 
    protected $db_user = 'user'; 
    protected $db_pass = 'pass'; 
    protected $db_host = 'localhost'; 
    protected $connection; 

    public function connect() { 
    $connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name); 

    // check connection 
    if ($connection->connect_error) { 
     trigger_error('Database connection failed: ' . $connection->connect_error, E_USER_ERROR); 
    } 

    } 

    public function resultToArray($result) { 
    $rows = array(); 
    while($row = $result->fetch_assoc()) { 
     $rows[] = $row; 
    } 
    return $rows; 
    } 

    public function sel($table) { 
    $q = "SELECT * FROM $table"; 
    $result = $this->connection->query($q); 
    $rows = $this->resultToArray($result); 

    return $rows; 

    $result->free(); 

    } 

} 
+0

也許你的意思是'$ this-> connection = new mysqli()..'而不是'$ connetion = new mysqli()...'然後,'return $ this ;' – Ghost 2014-09-20 04:53:52

回答

0

我想你應該設定一個構造函數,但是如果你不想要的,只是先返回它的一個實例,並設置你$this->connection屬性,而不是$connection (正常變量):

class DB { 

    protected $db_name = 'test'; 
    protected $db_user = 'test'; 
    protected $db_pass = 'test'; 
    protected $db_host = 'localhost'; 
    protected $connection; 

    public function connect() { 
     $this->connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name); 
     // ^^ this one, not $connection 
    // check connection 

    if ($this->connection->connect_error) { 
     trigger_error('Database connection failed: ' . $connection->connect_error, E_USER_ERROR); 
    } 

     return $this->connection; // then return this 
    } 

    public function resultToArray($result) { 
    $rows = array(); 
    while($row = $result->fetch_assoc()) { 
     $rows[] = $row; 
    } 
    return $rows; 
    } 

    public function sel($table) { 
    $q = "SELECT * FROM $table"; 

    $result = $this->connection->query($q); 
        //^so that if you call this, you have the mysqli object 
    $rows = $this->resultToArray($result); 

    return $rows; 

    $result->free(); 

    } 

} 

$db = new DB(); // instantite, 
$db->connect(); // then connect, shouldn't have to have this if you put the connection automatically on construct 
$result = $db->sel('users'); // feed a valid existing table name 
echo '<pre>'; 
print_r($result); 
1

使構造函數像

public $mysqli; 
    public function __construct() 
    { 
     $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 
     $this->mysqli = $mysqli; 
    } 

public function sel($table,$whr) 
    { 
    $query = "SELECT * FROM ".$table." where id='$whr'"; 
    $result = $this->mysqli->query($query); 
    $total = array(); 
    while($row = $result->fetch_assoc()){ 
      //print_r($row);die; 
     $total[] = $row; 
    }//print_r($total);die; 
    return $total; 
    } 
+0

你也可以看看我的連接文件http://codeinspired.wordpress.com/2014/03/07/how-to-make-oops-based-functions-file-in-php/ – truesource 2014-09-20 05:00:36