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

my query() returns an error that the table "Media" does not exist

Hi guys just wondering if anyone can help. When I run my code and check it out in the browser I get the following error: unable to retrieve results: SQLSTATE[HY000]: General error: 1 no such table: Media

my code in the file connection.php is as follows:

<?php

try{

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

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

}catch(Exception $e){

echo "unable to connect: ";

echo $e->getMessage();

}

try{

   $results = $db->query("SELECT title,category FROM Media");

echo "retrieved data";

}catch(Exception $e){

echo "unable to retrieve results: ";

echo $e->getMessage();

exit; 

}

I have also imported the file database.sql into my phpMyAdmin database. Could someone please help. I have no idea why the table "Media" is not being found by the $db object.

Diar Selimi
Diar Selimi
1,341 Points

There might be two reasons i think

  1. Did you select the db name ?
  2. Did you write your table name correctly ?

Hi Diar

Thanks for getting back. When I entered

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

within my editor it created a file called database.db within my directory. I have just deleted this file and replaced it with the database.db found in the project files folder.

The $db object seems to be able to access the table now. I am just wondering would you know what the database.db file is and how it's created?

Thanks again for the help.

1 Answer

Diar Selimi
Diar Selimi
1,341 Points

Check the .db files what they contain, there might be a problem with your content of you file, or just post it here.