0
下面的錯誤類函數是不完整/不正確的嗎?我永遠不能得到任何錯誤消息時查詢不正確,PHP:類顯示錯誤函數
#connects the database and handling the result
class __database {
protected $connection = null;
protected $error = null;
#make a connection
public function __construct($hostname,$username,$password,$database)
{
$this -> connection = new mysqli($hostname,$username,$password,$database);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
#fetches all result rows as an associative array, a numeric array, or both
public function fetch_all($query)
{
$result = $this -> connection -> query($query);
if($result)
{
return $result -> fetch_all(MYSQLI_ASSOC);
}
else
{
$this -> error = $this -> connection -> error;
return $this -> error;
}
}
#display error
public function get_error()
{
return $this -> error;
}
#closes the database connection when object is destroyed.
public function __destruct()
{
$this -> connection -> close();
}
}
public function get_error()
似乎是在我的DB類沒用......我看了一下PHP Exception
但我不知道如何將它進入上面的這個db類!請指教...
編輯:
我試圖改變代碼到這一點,
# return the current row of a result set as an object
public function fetch_object($query)
{
$result = $this->connection->query($query);
if($result)
{
...
}
else
{
__database::get_error();
}
}
和錯誤類函數,
#display error
public function get_error()
{
$this->error = $this->connection->error;
return $this->error;
}
因此,我認爲它應該觸發get_error()函數,但仍然沒有任何東西已顯示從錯誤函數...
+1更清潔 – HyderA
感謝。當我測試你的代碼時出現這個錯誤 - 致命錯誤:調用未定義的方法mysqli :: fetch_all()在C:\ wamp \ www \ ... \ class_database.php xxx行 - 任何想法? – laukok
已更新。但我的代碼純粹僅用於示例目的。 – RobertPitt