当试图使用PDO连接到错误的数据库时,PHP不会显示错误

时间:2022-10-13 11:58:14

I'm pretty much new to PHP and I'm having a very basic problem which I haven't found a solution to despite looking through similar questions in the forum.

我对PHP非常熟悉,我有一个非常基本的问题,我还没有找到一个解决方案,尽管在论坛中查找类似的问题。

I'm trying to connect PHP with my database (MySQL) through PDO. If I enter a wrong username or password php does show an error in the browser but if I enter a wrong database name it doesn't retireve any errors. How is this possible? My code is as follows:

我正在尝试通过PDO将PHP和数据库(MySQL)连接起来。如果我输入了错误的用户名或密码,php会在浏览器中显示错误,但是如果我输入了错误的数据库名,它不会恢复任何错误。这怎么可能?我的代码如下:

<?php
try{
    $conn = new PDO('mysql:127.0.0.1;dbname=myDb','root','root');
    $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOexception $e){
    echo 'wrong credentials';
}

My php.ini file is configured to show errors and Apache was restarted after modifying it with the following values:

我的php。ini文件配置为显示错误,Apache用以下值修改后重新启动:

error_reporting  =  E_ALL | E_STRICT
display_errors = On
display_startup_errors = On
log_errors = On

I also tried to use the following code at the begining of my script but it still doesn't show errors related to a wrong database name

在我的脚本开始时,我也尝试使用以下代码,但是它仍然没有显示与错误数据库名相关的错误

ini_set('display_errors', 1);
error_reporting(~0);

Thanks a lot in advance for your help, I'm sure is probably a pretty stupid thing but I honestly can't figure it out.

非常感谢你的帮助,我确信这可能是一件很愚蠢的事情,但我实在想不出来。

1 个解决方案

#1


4  

This turned into quite a tricky bug to find and explain even though it is quite simple.

这变成了一个相当棘手的bug,尽管它非常简单。

The issue is that the dsn string is incorrect.

问题是dsn字符串不正确。

current value: 'mysql:127.0.0.1;dbname=myDb'

当前值:“mysql:127.0.0.1;dbname = myDb '

it should be: 'mysql:host=127.0.0.1;dbname=myDB' note: host= was missing.

应该是:'mysql:host=127.0.0.1;dbname=myDB'注意:host=丢失。

However, PDO does not validate the dsn parameter string correctly and connects to the Localhost database without any error.

但是,PDO没有正确地验证dsn参数字符串,并且没有任何错误地连接到Localhost数据库。

However, it has ignored the dbname parameter, so that when you try a query then you get an error of: 1046 No database selected'.

但是,它忽略了dbname参数,因此当您尝试查询时,您会得到一个错误:1046没有选择数据库'。

Clarification of what is happening (see Alfredo Delgado below): PDO is ignoring the malformed parameter completely and assuming the Localhost database is what you want. ;-/

澄清正在发生的事情(请参阅下面的Alfredo Delgado): PDO完全忽略了格式错误的参数,并假定您需要的是本地主机数据库。,- /

As mentioned by Alfredo Delgado:

正如Alfredo Delgado提到的:

... "the resulting behavior of not having host= in the dsn was that PDO was silently defaulting to localhost. Since I happened to have a local copy of the DB I wasted hours chasing down red herrings like permissions."

…在dsn中没有主机的结果行为是PDO在静默地默认本地主机。因为我碰巧有一个本地的数据库拷贝,我浪费了很多时间去寻找权限之类的问题。

#1


4  

This turned into quite a tricky bug to find and explain even though it is quite simple.

这变成了一个相当棘手的bug,尽管它非常简单。

The issue is that the dsn string is incorrect.

问题是dsn字符串不正确。

current value: 'mysql:127.0.0.1;dbname=myDb'

当前值:“mysql:127.0.0.1;dbname = myDb '

it should be: 'mysql:host=127.0.0.1;dbname=myDB' note: host= was missing.

应该是:'mysql:host=127.0.0.1;dbname=myDB'注意:host=丢失。

However, PDO does not validate the dsn parameter string correctly and connects to the Localhost database without any error.

但是,PDO没有正确地验证dsn参数字符串,并且没有任何错误地连接到Localhost数据库。

However, it has ignored the dbname parameter, so that when you try a query then you get an error of: 1046 No database selected'.

但是,它忽略了dbname参数,因此当您尝试查询时,您会得到一个错误:1046没有选择数据库'。

Clarification of what is happening (see Alfredo Delgado below): PDO is ignoring the malformed parameter completely and assuming the Localhost database is what you want. ;-/

澄清正在发生的事情(请参阅下面的Alfredo Delgado): PDO完全忽略了格式错误的参数,并假定您需要的是本地主机数据库。,- /

As mentioned by Alfredo Delgado:

正如Alfredo Delgado提到的:

... "the resulting behavior of not having host= in the dsn was that PDO was silently defaulting to localhost. Since I happened to have a local copy of the DB I wasted hours chasing down red herrings like permissions."

…在dsn中没有主机的结果行为是PDO在静默地默认本地主机。因为我碰巧有一个本地的数据库拷贝,我浪费了很多时间去寻找权限之类的问题。