Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PDO Exception loads so slow!

Hi guys! I'm following the course to connect php to a database and when it comes to load the exception error (during "Handling Exceptions" lesson) it takes something like a minute before my browser loads it. Someone knows on what may it depend? Thank You very much!

3 Answers

Can you copy your code right here?

I don't think there is a problem with the code itself but with the way PDO handles the connection trials to the database. I mean it's like it is trying to connect over and over before giving up and run the exception!

<?php
try {
    $db = new PDO("mysql:host=localhost;dbname=shirts4mike;","root","ffredda");
    $db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db -> exec("SET NAMES 'utf8'");
} catch (PDOException $e) {
    echo "Could not connect to the database.";
exit;
}
?>

Again, thank you!

I checked my access.log and errors.log and the exception in thrown only two minutes after trying to access the database with the wrong username!

"[Sun Nov 03 15:23:31 2013] [error] [client 127.0.0.1] PHP Fatal error: Uncaught exception ' PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'loclalhost' (110)' in /home/fabrizio/public_html/sito1/database.php:5\nStack trace:\n#0 /home/fabrizio/public_html/sito1/database.php(5): PDO->__construct('mysql:host=locl...', 'root', 'ffredda')\n#1 {main}\n thrown in /home/fabrizio/public_html/sito1/database.php on line 5"

There must be something that can limit the number of attempts to throw the exception earlier! I don't understand if this is a problem of the apache2 local server, of mysql or of the PDO class in php! Some teacher could help me understand this? I'm not sleeping on this issue, I MUST understand it!

limiting the wait_timeout variable in mysql is useless..so it shouldn't be mysql falult...

...don' t really know...-.-