我想學習更多的PHP,OOP和MySQLI,但我被卡住了。谷歌沒有幫助我,在這個網站上搜索也沒有給我任何結果。我有一個配置類連接到數據庫,並在另一個類我要運行一些查詢,但我得到這個錯誤與任何我想:簡單的PHP OOP和MySQLI
Fatal error: Call to a member function query() on a non-object in test.php on line 9
有人可以幫我嗎?
的config.php:
<?php
class Database
{
private $host;
private $user;
private $password;
private $db;
private $mysqli;
function __construct() {
$this->host = "private";
$this->user = "private";
$this->pass = "private";
$this->data = "private";
$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->data);
}
public function query($query)
{
return $this->mysqli->query($query);
}
}
?>
test.php的:
<?php
class Dummy
{
private $Database;
function __construct()
{
$this->Database = new Database();
$this->Database->test = $this->mysqli->query("SELECT test FROM test")->fetch_object()->test;
}
}
?>
的index.php:
<?php
require 'config.php';
require 'test.php';
$test = new Database();
$test = new Dummy();
echo $this->Database->test;
?>
什麼/在哪裏是'gw2Database'? – samayo
檢查出該行。在當前('Dummy')類中查看你稱之爲'query'的事物 - >它是一個'mysqli'對象。但沒有什麼是所謂的。所以它不存在。 – Nanne
從[這個回購](http://github.com/simon-eq/pdowrapper)學習,因爲我認爲你做錯了,如果你在你的課堂上硬編碼查詢 – samayo