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
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 Pointstry block not working
I still get the ugly php error appearing, despite using a try block. Here is my code:
<?php
try {
$db = new PDO("mysql:host=brokenhost;dbname=shirts4mike;port=3306","root");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
};
echo "Woo-hoo!";
I have put in "brokenhost" to test the exception handling, like it said to do in the video. The exception message is echoed to the screen correctly, but the PHP error still preceeds it. I am doing this using Google Chrome, if that makes any difference
5 Answers
Michael De Marre
14,198 PointsIt looks like you forgot to include a password for the database user. The root password usually has a password of root.
<?php
try { $db = new PDO("mysql:host=brokenhost;dbname=shirts4mike;port=3306","root","root");
} catch (Exception $e) { echo "Could not connect to the database."; exit; };
echo "Woo-hoo!";
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsThanks, although including the extra "root" caused an error for me (I don't have a password for this database).
However, this wasn't quite the problem I was concerned with. I do not understand why a PHP error is appearing, as well as my custom exception message. The server should "try" the code to prevent an error showing and then show the custom error message instead.
Michael De Marre
14,198 PointsWhat server emulating tool are you using?
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsWAMP on Windows
thomascawthorn
22,986 PointsCan you copy and paste your errors?
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsSure Tom, the error message is:
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\shirts4mike\inc\database.php on line 4
And line 4 is:
$db = new PDO("mysql:host=brokenhost;dbname=shirts4mike;port=3306","root");
I've deliberately written "brokenhost" instead of "localhost" to check the error handling - which isn't working because the php error is still showing. The "Could not connect to the database." message is also showing, so the catch clause is working, but the try clause is still running when it should be tried and then if it fails it should not be run and the catch clause should be run instead (if I understand things correctly)
For reference here is the entire code:
<?php
try {
$db = new PDO("mysql:host=brokenhost;dbname=shirts4mike;port=3306","root");
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
}
thomascawthorn
22,986 PointsUnless I haven't' understood correctly, isn't this catch working fine?
There's a warning in your try block - php tells you about the warning - (note: warning, not error). Then an exception is thrown - probably because a connection wasn't made (because you're using purposely incorrect credentials and as the warning told you, the host doesn't exist), and you catch the exception. This exception is then displayed on the screen.
Have you tried turning off warnings?
So what happens if you use the correct credentials?
this post might be of interest is you're looking to stop those warnings.