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

PDO failure

I have been working on learning how to correctly interact with a database using php. The site I am building uses PDO and for some reason nothing is working correctly. Try catch block which echo nothing connections failing with no errors. I just need help

<?php
$title = "Portfolio";
$headerMessage = $title;

include("Includes/headerAndSideBar.php");
?>
<h1>Our Work</h1>
<ul id="projects">
    <!-- Process should be repeated in PHP -->

    <?php
    include("Includes/dbConnect.php");

    if (!empty($_GET)) {
        try{
            $connection = $db->prepare("SELECT * FROM `content` WHERE `pageId` LIKE ?");
            $connection->bindValue($_GET["page"], PDO::PARAM_STR);
            $connection->execute();
            $row = $connection->fetch();

            $name = $row["name"];// name of Project
            $thumb = $row["thumb"];// file location of thumbnail
            $screenshots = explode(",", $row["screenshots"]);// multiple file locations of screenshots
            $descriptions = explode(",", $row["descriptions"]);// description of the parallel screenshot
            $request = $row["request"];// Request from customer
            $link = $row["link"];// URL to site

            echo "<h2>" . $row["name"] . "</h2>";
            echo "<img src='/static/images/portfolio/" . $row["thumb"] . "' alt='" . $row["name"] . "'>";
        } catch (Exception $e){
            echo "Error";
            echo $e;
        }
    }
    else {
        try {
            foreach ($db->query("SELECT * FROM content") as $row) {
                echo "<li>";
                    echo "<a href='?page=". $row["pageId"] ."' title='". $row["name"] . "'>";
                        echo "<img src='/static/images/portfolio/". $row["thumb"] . "' alt='". $row["name"] . " Logo'>";
                    echo "</a>";
                echo "</li>";
            }
        } catch (Exception $e){
            echo "Error";
            echo $e;
        }
    }
    ?>
</ul>

<?php
include("Includes/footer.php");
?>

2 Answers

I always like to explicity bind values for readability:

see example one

<?php


$connection = $db->prepare("SELECT * FROM `content` WHERE `pageId` LIKE :pageID");

$connection->bindValue(':pageID', $_GET['page'], PDO::PARAM_STR);

I would start with binary chopping at EVERY step and make sure you're getting to the correct places and all your initial variables are set.

<?php

echo "<pre>";
var_dump($object);
exit;

I would make sure:

  • You're hitting the correct if/else.
  • Your variables are being set properly - I notice you're saying if $_GET exists, execute this block - but you're not checking + if $_GET['page'] exists before using it.
  • If you try and concatenate the pageID into the query string without binding - does this work?
  • Can you echo the query string and does it appear correctly with concatenation?

If i'm not mistaken, a try/catch will catch any failure from the try block at any point? You could try commenting out pieces of code to narrow down where the exception is being thrown.

You've probably checked all this stuff already, I'm just throwing out ideas!

Hope something helps..

Thanks your trouble shooting Idea fixed everything. I realized my querys were incorrect and returning NULL.

Thanks, --Ricky

Not sure if this might be the issue, but from what I recall, there is no need to use the " ` " character in your prepare statement. So, instead of "SELECT * FROM `content` WHERE `pageId` LIKE ?", use "SELECT * FROM content WHERE pageId LIKE ?".

Thanks I'll try it!

--Ricky