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 Querying the Database with PHP Querying the Database

Siraj Khan
Siraj Khan
3,451 Points

What is wrong with my code.? Please help.

Why do I see only "Unable to retrieve results".? Even that everything is in place. Below is my code:

<?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();
  exit;
}

try {
  $reults = $db-> query("SELECT title, category FROM Media");
  echo "Retrieved Results";
} catch (Exception $e) {
  echo "Unable to retrieve results";
  exit;

}

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It looks like you haven't chained your query to the database properly. Also try fixing the results variable to say $results which I'm presuming is a typo.

So

<?php
$reults = $db-> query("SELECT title, category FROM Media");

?>

becomes

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

?>

I think PHP is thinking they're 2 separate statements. :-)

Siraj Khan
Siraj Khan
3,451 Points

Hey Jonathan Grieve ,

What do you mean by "It looks like you haven't chained your query to the database properly" .?

And I tired this as well:

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

?>

But nothing seems to work.

Siraj Khan
Siraj Khan
3,451 Points

However I see this long error/Warning message in phpMyAdmin: "Warning in ./libraries/classes/Dbi/DbiMysqli.php#213 mysqli_query(): (HY000/1682): Native table 'performance_schema'.'session_variables' has the wrong structure"

I don't understand what's going on.! Please help

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Are you still getting the "unable to retreve results" error? Because that message suggests that you are at least getting a conenction to the database.

Try uncommenting and adding the following code to your try blocks

//  echo $e->getMessage();

I suspect then the next thing to try would be to check you're making the right query to the database. :-)

Good luck!

Siraj Khan
Siraj Khan
3,451 Points

I tried uncommenting echo $e->getMessage(); and I still get the same thing in the browser i.e. "unable to retrieve results".! What do you mean by "making the right query to the database".??