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 PHP & Databases with PDO PDO Queries & Results Working With Results

My PHP code is block my HTML

When the top php code in index.php is on, its blocking my HTML code from running. When I comment out the code, it shows the HTML. But when I dont, its doesnt work.

Here is my index.php code:



try { $results = $db->query('select * from film'); } catch(Exception $e) { echo $e->getMessage();


$films = $results->fetchAll(PDO::FETCH_ASSOC));


And here is my database.php code:


// remove before flight ini_set('display_errors', 'On');

try {

$db = new PDO('sqlite:./database.db'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(Exception $e) {

echo $e->getMessage(); die();



Paulius Vitkus
Paulius Vitkus
24,230 Points

I had the same problem:

If you write everything in double quotes ", not '. Then on echo line write this:

echo "<li><i class=\"lens\"></i>Film One</li>";

Problem solved!

2 Answers

kabir k
kabir k
Courses Plus Student 18,036 Points

If I understand your question correctly, I believe the reason your php code in index.php is blocking (or preventing) your HTML code from running is because of the die(); statement inside the top PHP code block or tags.

The die(); statement stops the rest of the file (the subsequent HTML code) from loading (i.e. it is used die out the script)

I believe the teacher did that on purpose so that we can, for that moment, focus on the PDOStatement object returned from the database and be able to see and work with the results (thereby reducing confusion between the outputs from the PHP code and the HTML code)

I don't fully understand your question. Do you have separate files for index.html and index.php? If not, where is the html?

Is this in Workspaces? If so, please post a snapshot by using the camera icon on the upper right side of the workspace. Generate and open the workspace and post the link here.

Also, please refer to the Markdown Cheatsheet to learn how to properly format your code quotes.

I do see some potential issues with your code, although I use MySQL, not SQLlite. Here is the connection to the database code I use for a development project.

try {
    $db = new PDO("mysql:host=localhost;dbname=cubScout;charset=utf8mb4","root","");

} catch (Exception $e) {
    echo $e->getMessage() . " dbCall Line 13";

I am not sure about the :./database.db section of your code. It is not right for MySQL. You do not have a user name or password. My code has a user name of "root" and "" for the password. That will change when it goes live. I like to add more information to the catch as you can see above. It tells me where it failed.

Here is the code for my query:

function full_scout_list() {
//  require(ROOT_PATH . "inc/database.php");

    try {
        $results = $db->query("
                SELECT FirstName, LastName, Den, Bobcat, Tiger, Wolf, Bear, Webelo, Arrow, Health, id
                FROM scouts
                ORDER BY Den DESC, LastName");
        $scouts = array(array());
        $scouts = $results->fetchAll(PDO::FETCH_ASSOC);
    //  $recent = array_reverse($recent);
        return $scouts;
    } catch (Exception $e) {
        echo $e->getMessage() . "db.php full_scout_list Line 52";

The primary difference I see between our codes is that I set the variable that the results are going to as an array before the fetchAll. I seem to recall that this was required from the video, but may be making that up.

You don't return your results, but it is not set up as a function, so that should not be a problem.

If you can elaborate on the problem more, I may be able to help you more.