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 Retrieving the Result Set

After running the code, the page showed Null, which meant the database.db file is empty?

After running the code, the page showed Null, which meant the database.db file was empty? Also I couldn't open the database.db file directly. No match application. Have tried downloading new projects, still didn't work. Cannot figure out why? Please Help. Thanks

Here is the 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"; exit; }

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

var_dump ($results);

?>

Lola Jahn
Lola Jahn
18,718 Points

Your DIR in the first startement is wrong. It needs to look like this:

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

1 Answer

Jeremy Antoine
Jeremy Antoine
15,785 Points

Found the Answer! Your error is in this line where you define "$results".

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

You are setting your variable to "$result" (without an 's'), but then you are trying to dump the variable "$results" (with an 's').

It is showing NULL because in your code, the variable "$results" does not exist, instead "$result" exists because of the missing 's'.