php - Mysql PDO 无效凭据导致内部服务器错误而不是捕获错误

标签 php mysql pdo internal-server-error

我比任何事情都更困惑,我试图使用 PDO 连接到我的 mysql 数据库。我有无效的凭据,而不是我的 try and catch 捕获错误并告诉我“无效的凭据”,它完全卡住了页面,我收到了这个错误:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

我的代码如下:

function pdo_connect(){
    $host           = 'localhost';
    $user           = 'user';
    $pass           = 'wrongpass';
    $dbSchema       = 'wrongschema';
    try{
        $dbcPDO = new PDO('mysql:host='.$host.';dbname='.$dbSchema, $user, $pass); //connect to the PDO instance
        $dbcPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //turn on errors
        return $dbcPDO;
    }catch(PDOException $e) {
        trigger_error('Could not connect to the database: ' . $e->getMessage());
        return false;
        exit();
    }
}

当我有正确的凭据时,我可以毫无问题地连接。我会假设无效的凭据会被 PDOException 捕获,并且会显示连接失败的消息。

最佳答案

仅供引用,当我们对数据库使用无效凭据时,PDO 将捕获异常。不确定是什么导致了您的问题。

  1. 您在函数中编写了连接到数据库的 PDO。确保在 PHP 脚本中调用该函数。

  2. 您可以使用下面的代码来检查,PDO 连接到数据库是否成功。

    if($con==null)
    {
     $response["connected"] = "false";
    }else{
     $response["connected"] = "true";
    }
    echo json_encode($response);     // $con is the connection object returned.
    
  3. 您在上面使用的代码在我的服务器上运行良好。问题可能出在服务器本身。尝试检查错误日志的更多详细信息。

Link将指导您了解有关内部服务器错误的信息。也许您可以发布错误日志以快速解决问题。

祝你好运..!!

关于php - Mysql PDO 无效凭据导致内部服务器错误而不是捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907032/

相关文章:

mysql - 限制 SQL 语句 (MySQL) 中的特定 WHERE 子句?

php - 是否可以仅对部分脚本禁用错误报告?

mysql - Laravel 对用户模型的原始查询

PHP PDO SQLITE - 连接

mysql - DATE_SUB 日期间隔

mysql - 使用 vanilla sql 代码与 upsert 相比有优势吗

php - 包含数组的内爆多维数组

php - 简单的 PHP 不填充页面

javascript - 如何序列化两个表格和整个表格

php - 如何仅序列化一对多关系的子集?