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 Integrating PHP with Databases Querying the Database with PHP Querying the Database

Volodymyr Danileichenko
Volodymyr Danileichenko
7,354 Points

query() method issue

I'm using MySQL and the try/catch construction from the video doesn't work. $db -> query() method returns boolean. So the code is executed in any case. It just returns "false" in case of an error. Is it a correct behaviour or am I doing something wrong? Thanks

Simon Woodard
Simon Woodard
6,545 Points

Perhaps you could post the code you've written so we have a better idea of the issue.

2 Answers

Simon Coates
Simon Coates
28,694 Points

Does the code you include to get a connection include

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

? I think Alena said that you had to tell it to throw exceptions.

try wrapping the Media word in these ``

Like this -

try { $db->query("SELECT title, category FROM Media"); } catch (Exception $e) { echo "Bad query"; }