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 Databases and PHP Handling Exceptions

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Try catch always returns successful connection

Hi all.

Did we find a common reason for this problem? I've followed the video and have the same code as Alena.

<?php

try {
    $db = new PDO("sqlite:" . __DIR__ . "/database.db");
    var_dump($db);
} catch (Exception $e) {
    echo "Unable to connect to the database";
    exit;

}

echo "Connected to the database successfully";
?>

Alena did mention there are subtle differences between PHP7 and PHP5 could something be affecting this somehow? Is it a server configuration that's installed in Workspaces that lets it work but not locally or on another server?

Thankss

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Okay,

In case anybody reads this I'm not sure if this issue is solved yet or not. But I do know that adding the follow code per the video...

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

allows me to move forward. I still don't get the echoed message from the try catch block when I make an incorrect file name and it still creates the empty file but I do know by attempting to add a query to the connection.php file, using a second try-catch block, it will not find the "Media" table so I know that it's not finding the data and changing it back confirms that the data is there.

Again this is only my theory but for now it works. :-)

Had the same problem. Followed the tutorial exactly, and it always returned succesful. But I found out that not using the exact faulty db-name Alena used in her demonstration ("/databaaase.db") helps: there seems to be an actual existing database "/databaaase.db". Using any other databasename, like "/databasexxx.db" DOES return the error. Treehouse: for future learners, it would be helpful to delete or rename the database "/databaaase.db"!

Riki Tiilikainen
Riki Tiilikainen
4,042 Points

I'm having the same problem, keeps creating new databases.