下面是使用(試圖)從數據庫中提取信息但它給我一個錯誤的文件。PHP數據庫類錯誤
配置文件:
define("DBHOST", "127.0.0.1");
define("DBUSER", "root");
define("DBPASS", "root");
define("DBNAME", "test");
define("DBPORT", "3306");
DB類:
class Database{
public function __contruct(){
$con = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME, DBPORT);
if (!$con){
die("Error connecting to database");
}
else{
$this->con = $con;
}
}
function SimpleQuery(){
$query="SELECT * FROM accounts";
$array = $this->con->query($query);
while ($fields = $array->fetch_array(MYSQL_ASSOC)){
$values[] = $fields;
}
return $values;
}
}
索引文件:
require ("config.php");
require ("database.php");
$db = new Database();
echo $rows = $db->SimpleQuery();
給了我這個錯誤:
Fatal error: Call to a member function query() on a non-object in database.php
我認爲你應該在課堂中聲明$ con。 http://php.net/manual/en/language.oop5.properties.php –
這些東西沒有工作:( – Phentom
@Phentom這是一個錯字,你有'__contruct'而不是'__construct'。另外,你應該檢查對於連接錯誤,使用'$ this-> con> connect_errors'(給出更多細節爲什麼連接失敗) – Bjorn