我比任何事情都更困惑,我试图使用 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 将捕获异常。不确定是什么导致了您的问题。
您在函数中编写了连接到数据库的 PDO。确保在 PHP 脚本中调用该函数。
您可以使用下面的代码来检查,PDO 连接到数据库是否成功。
if($con==null) { $response["connected"] = "false"; }else{ $response["connected"] = "true"; } echo json_encode($response); // $con is the connection object returned.
您在上面使用的代码在我的服务器上运行良好。问题可能出在服务器本身。尝试检查错误日志的更多详细信息。
Link将指导您了解有关内部服务器错误的信息。也许您可以发布错误日志以快速解决问题。
祝你好运..!!
关于php - Mysql PDO 无效凭据导致内部服务器错误而不是捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907032/