1

I have this code

try {
    $dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', '');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage();
}

And it gives me the exception message:

SQLSTATE[HY000] [1049] Unknown database 'db_informations'

Because the correct name of my database is db_information only.

My question is, even if I don't include the line:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I still get the same exception and I think it's not necessary to use it? Is it?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
wobsoriano
  • 12,348
  • 24
  • 92
  • 162

1 Answers1

2

This is simply because that's the behaviour of PDO::__construct() as you can read in the manual:

PDO::__construct() throws a PDOException if the attempt to connect to the requested database fails.

But if you don't set the error mode to Exception and you do:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', '');
    $dbh->query("SELECT * FROM aTableWhichDoesNotExists");
} catch(PDOException $e) {
    echo $e->getMessage();
}

You won't get any excpetion message or error, because you didn't set the error mode. So you need to do this:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=db_informations', 'root', '');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->query("SELECT * FROM aTableWhichDoesNotExists");
} catch(PDOException $e) {
    echo $e->getMessage();
}

To receive an exception, which you then can catch:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.atablewhichdoesnotexists' doesn't exist


Also if you just think logically:

setAttribute() needs to be used with ->, which means you need an instance of the class to call that method. So how would you be able to call that method, if the instance couldn't be created correctly?

(So that would mean setAttribute() would have to bee static, so that you can set something/call it before you take the instance of the class)

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • @FewFlyBy You're welcome. (Just updated my answer with just logical thinking) – Rizier123 May 31 '15 at 13:26
  • What is the instance of the class there sir? Because I'm also having problems differentiating -> from :: even if I read php oop docs.. – wobsoriano May 31 '15 at 13:54
  • @FewFlyBy You create a *new* instance of a class with the keyword [**new**](http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new) so here you (try to)create an instance of PDO with: `new PDO()` – Rizier123 May 31 '15 at 13:56
  • @FewFlyBy Maybe also take a look here: http://stackoverflow.com/q/3737139/3933332 There are some good questions about `::` and `->` – Rizier123 May 31 '15 at 13:57
  • so I created a new instance of the PDO class right? – wobsoriano May 31 '15 at 13:59
  • @FewFlyBy Exactly, and if you do: `var_dump($dbh);` you will see that it is an instance of the class PDO: `object(PDO)` (It's the same as you would do: `$o =` **new** `StdClass(); var_dump($o);` you will see, that `$o` is an instance of the class StdClass -> `object(stdClass)`) – Rizier123 May 31 '15 at 14:00
  • Yes I understand it now, thanks. And so the scope resolution operator :: is used to access method of a class right? – wobsoriano May 31 '15 at 14:04
  • @FewFlyBy `::` yes, but not only methods. It's used to only access **static** properties and methods. – Rizier123 May 31 '15 at 14:05
  • Alright, because for example PDO::getAvailableDrivers();, you don't even need to create a new instance of a class to use it – wobsoriano May 31 '15 at 14:08
  • 1
    @FewFlyBy Exactly, you got it! (Just don't get confused of the way php.net writes their prototypes: `public static array PDO::getAvailableDrivers ( void )` <- The `::` in the prototype doesn't mean that it is static, since php.net uses it for every class method. <- But the keyword **static** in the prototype will give away, that the method is static and called with `::`) – Rizier123 May 31 '15 at 14:10