我正在使用XAMPP,我想檢測MySQL數據庫是否正在運行。我的代碼如下所示:如何檢測MySQL數據庫是否正在運行?
$this->connection = mysqli_connect(
$this->host,
$this->name,
$this->pass,
$this->db
);
if ($this->connection->connect_errno)
{
$this->connection = false;
return false;
} else { $this->connection->set_charset('utf-8'); }
我收到以下日誌:
PHP Warning: mysqli_connect(): (HY000/2002): Connection refused ...
PHP Notice: Trying to get property of non-object in // This refers to $this->connection->connect_errno
PHP Fatal error: Call to a member function set_charset() on boolean
我如何避免這種情況?我如何檢查一般數據庫是否可用?
在'try/catch'語句中執行連接,以便您可以優雅地處理錯誤。 –
CAn你展示全班? – Mihai