從我在線找到的教程構建。簡單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();
}
}
也許你的意思是'$ this-> connection = new mysqli()..'而不是'$ connetion = new mysqli()...'然後,'return $ this ;' – Ghost 2014-09-20 04:53:52