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 Limiting Records in SQL Adding Pagination Links

Zachary Baker
PLUS
Zachary Baker
Courses Plus Student 11,504 Points

Undefined variable in php

Bad Query Notice: Undefined variable: result in /home/treehouse/workspace/inc/functions.php on line 25

Fatal error: Uncaught Error: Call to a member function fetchColumn() on null in /home/treehouse/workspace/inc/functions.php:25 Stack trace: #0 /home/treehouse/workspace/catalog.php(27): get_catalog_count('books') #1 {main} thrown in /home/treehouse/workspace/inc/functions.php on line 25

Here is a snapshot of my workspace: https://w.trhou.se/j3g7fmo250

2 Answers

Matthew Carson
Matthew Carson
5,965 Points

In your catalog.php file you are setting $section here:

<?php
if (isset($_GET["cat"])) {
    if ($_GET["cat"] == "books") {
        $pageTitle = "Books";
        $section = "books";
    } else if ($_GET["cat"] == "movies") {
        $pageTitle = "Movies";
        $section = "movies";
    } else if ($_GET["cat"] == "music") {
        $pageTitle = "Music";
        $section = "music";
    }
}

That only sets $section if a $_GET['cat'] is set, which is passed through the query string like this:

http://name-of-app.dev/catalog.php?cat=books

But, when viewing the full catalog you aren't passing a cat.

http://name-of-app.dev/catalog.php

So $section is never getting set, since the if (isset($_GET["cat"])) conditional doesn't pass.

Initialize it to an empty string towards the top of the file, like so:

<?php 
include("inc/functions.php");

$pageTitle = "Full Catalog";
$items_per_page = 8;
$section = '';

Now get_catalog_count() function will grab the count of all media regardless of its category since the category parameter being passed to it is empty.

Zachary Baker
Zachary Baker
Courses Plus Student 11,504 Points

Thank you again for the help. I really appreciate it! I have one more problem and I think the page will be functioning properly. I have my full catalog set up, but when I select media, books or music, The page paginates, but no items show up. How do I correct this?

here is my updated workspace: https://w.trhou.se/h19c5je8ep

Matthew Carson
Matthew Carson
5,965 Points

Inside the get_catalog_count() function in inc/functions.php:

Are you ready for this? Change this code:

<?php
$result = $db->prepare(
        $sql. 
        "WHERE LOWER(category) = ?
        "
      );

to this:

<?php
$result = $db->prepare(
        $sql. 
        " WHERE LOWER(category) = ?
        "
      );

There needs to be a space in the SQL statement otherwise to would be interpreted as:

SELECT COUNT(media_id) FROM MediaWHERE LOWER(category) = ?

Instead of:

SELECT COUNT(media_id) FROM Media WHERE LOWER(category) = ?
Zachary Baker
Zachary Baker
Courses Plus Student 11,504 Points

Thank you for the help, Now I am having trouble adding pagination to my full catalog. I have corrected most of the issues... but now I'm stuck on an undefined variable on line 27 and line 82. It keeps telling me that my section variable is undefined. How do I correct this?

here is an updated workspace: https://w.trhou.se/bdawa3szle