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

working on this via localhost with xamp, but not getting a connection to the database?

I have been using xampp, I have the correct try / catch statements, I have double checked my code and am getting an error in my second catch... 'unable to retrieve the results' which is in the statement getting the results: $results = $db->query("SELECT title, category FROM Media");

Am I supposed to have the database already created and calling it right now? I noticed when I hit this page it creates a database file cuz of this statement:

$db = new PDO('sqlite:'.DIR.'/database.db');

Any ideas on what I'm doing wrong?

Paul Yorde
Paul Yorde
10,497 Points

Hi sorry I'm not familiar with the project you are working on, but is there something in the instructions that have told you that a database has already been created.? If not, then you will need to create a database before you can you use it. Treehouse has several courses on doing that.

Sorry Paul, I think I posted this in the wrong forum? It's for this track:

https://teamtreehouse.com/library/integrating-php-with-databases

They say to use SQLITE and if you echo out var_dump($results->fetchAll(PDO::FETCH_ASSOC)); you're supposed to be given the arrays back from the php site with all the paths for the images and descriptions for the albums and movies and books. I may have overlooked that there was a database but it didn't really clarify that. You would think they would say something about it.

Paul Yorde
Paul Yorde
10,497 Points

Hi Jordon, so for those videos, you'll be using 'workspace' to do the project (at least to start). They already have the database set up for you which you'll see in the video 'getting started with PDO'. This line is using the database that they have set up for you, but it will only work in 'workspace' from what I can tell, so you can't use your own editor in case you were:

        $db = new PDO('sqlite:'.DIR.'/database.db');

So starting from the video, 'getting started with PDO', you should be able to follow along on workspace and see it working. this is the code I used for that video in workspace and it works:

       $db = new PDO('sqlite:'.__DIR__.'/database.db');
       var_dump($db);

Make sure that you have created the connection.php file in inc folder. This is an excellent learning tool by the way so I would encourage you to keep at it. One thing I notice about your code is that you're missing the underscores in the magic constant DIR path like this

        __Dir__

As far as your query statement goes, I would check and recheck every dot and every semicolon, etc. until its working. 9 times out of 10 it is a syntax error that we make

Best ~