Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,692 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"; }