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

Riki Tiilikainen
Riki Tiilikainen
4,042 Points

Database connection keeps creating a new database

This tutorial works in the workspaces environment, but not when I test locally and I can see that a few other people have had the same problem, but no solution posted. When I test the connection, locally using MAMP it creates a new database whenever I break the connection. Does anyone know why this is happening? Also, won't let me post an underscore by the DIR here! AH


try{ $db = new PDO ("sqlite:". _ _ DIR _ _ ."/database.db"); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); var_dump($db); } catch (Exception $e){ echo 'Caught exception: ', $e->getMessage(); exit; } echo "Connected to Database";


2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Riki Tiilikainen ! This is now the expected functionality. In newer versions of PHP/SQLite drivers, if the database cannot be found, it creates a new database for you. However, if you are working with legacy code, this may not be the case. There can also be other reasons for a connection to not be established.

On your local machine and using newer versions of PHP, you should expect that if the database file cannot be found, another database file matching the one you're trying to connect to will be created for you.

Hope this helps! :sparkles:

Hello Jennifer and Treehouse Team :smiley: it would be great to add this information to the teacher' s notes or to update the video. :pray: Thank you