4

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.

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:

<?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:

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.

3
  • 1
    What does $conn->errorInfo() show you? Commented Apr 15, 2015 at 1:06
  • 2
    Have you tried looking in the Apache error log? Commented Apr 15, 2015 at 1:43
  • 1
    @RyanVincent Please go ahead and post this as the answer. Commented Apr 15, 2015 at 4:05

1 Answer 1

4

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

The issue is that the dsn string is incorrect.

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

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

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

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

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. ;-/

As mentioned by 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."

11
  • This is also related to 'other issues' with PDO as regards 'PDO::ATTR_EMULATE_PREPARES, false'. It stops exceptions being raised with certain versions of PHP and PDO. I need to check the later versions. see Commented Apr 18, 2015 at 22:21
  • 1
    I ran into this, today. 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. Commented Jun 21, 2018 at 22:56
  • 1
    @AlfredoDelgado, you have my sympathy. ;-/ As the error report is incorrect it really is an amazing 'timesink' trying to find and explain what is actually wrong. .Updated the answer. Commented Jun 22, 2018 at 12:33
  • 1
    @RyanVincent, yes I do have 'spaces', but are spaces regarded in those particular places in PHP programing? Commented May 30, 2020 at 1:06
  • 1
    @RyanViçeñte, oh yes I can access database from PHPMyAdmin! Commented May 30, 2020 at 6:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.