MySQLi在连接失败时抛出PHP错误

时间:2013-09-15 19:08:09

标签: php error-handling mysqli

我写了一些代码来连接到我的数据库,它在我的Windows计算机上运行时效果很好:

<?php
    include_once(__DIR__ . '/config.php');

    $sqlc = new mysqli('p:' . DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if ($sqlc->connect_error)
    {
        die('Unable to connect to server or database.');
    }
?>

如果连接成功,一切都按预期工作,当连接失败时,它会输出正确的错误消息(Unable to connect to server or database.)。

但是,当我将此文件上传到我的Raspberry Pi(Arch Linux,最新的Apache,PHP和MariaDB)时,如果连接不成功,PHP脚本将失败。我尝试在配置中启用display_errors并输出此错误:

Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'raspi' in /srv/http/tools/sqlconnect.php on line 4

如何让我的PHP脚本再次输出错误消息(Unable to connect to server or database.)?

1 个答案:

答案 0 :(得分:1)

试试这个

$sqlc = new mysqli('p:' . DB_HOST, DB_USER, DB_PASS, DB_NAME) or die('Unable to connect to server or database.');