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 Setting LIMITs

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

Parse error: syntax error, unexpected '$results' (T_VARIABLE) in /home/treehouse/workspace/inc/functions.php on line 47

I have a Parse error on line 47 of my function.php file. I followed the video and I tried to fix the problem, but to no avail, I get the same thing. What do I do from here?

Zachary Baker
Zachary Baker
Courses Plus Student 11,504 Points
<?php

function get_catalog_count($category = null) {
  $category = strtolower($category);
  include("connection.php");

  try {
    $sql = "SELECT COUNT(media_id) FROM Media";
    if(!empty($category)) {
      $result = $db->prepare(
        $sql. 
        "WHERE LOWER(category) = ?
        "
      );
      $result->bindParam(1, $category, PDO::PARAM_STR); 
    } else {  
      $result = $db->prepare($sql);
    }  
    $result->execute();
    }
    catch (Exception $e) {
      echo "Bad Query";  
    }

  $count = $result->fetchColumn(0);
  return $count;  
}

function full_catalog_array($limit = null, $offset = 0) {
  include("connection.php");
  try {
      $sql = "SELECT media_id, title, category, img 
        FROM Media
        ORDER BY
        REPLACE(
          REPLACE(
              REPLACE(title, 'The ', ''),
              'An', ''
              ),
            'A ', ''
          )";
    if (is_integer($limit)) {
      $results = $db->prepare($sql. " LIMIT ? OFFSET ?");
      $results->bindParam(1,$limit,PDO::PARAM_INT);
      $results->bindParam(2,$offset,PDO::PARAM_INT);
    } else {
      $results = $db->prepare($sql);
      }        
      $results->execute();
  } catch(Exception $e) {
    echo "Unable to retrieve results";
    exit;
  }
  $catalog = $results->fetchAll();
  return $catalog;
  }  

function category_catalog_array($category, $limit = null, $offset = 0) {
  include("connection.php");
  $category = strtolower($category);
  try {
      $sql = "SELECT media_id, title, category, img 
        FROM Media
        WHERE LOWER(category) = ?
        ORDER BY
        REPLACE(
          REPLACE(
              REPLACE(title, 'The ', ''),
              'An', ''
              ),
            'A ', ''
          )";
     if (is_integer($limit)) {
      $results = $db->prepare($sql. " LIMIT ? OFFSET ?");
      $results->bindParam(1,$limit,PDO::PARAM_INT);
      $results->bindParam(2,$offset,PDO::PARAM_INT);
     } else {
      $results = $db->prepare($sql);
      $results->bindParam(1, $category, PDO::PARAM_STR);
      $results->bindParam(2,$limit,PDO::PARAM_INT);
      $results->bindParam(3,$offset,PDO::PARAM_INT);
     } 
      $results->execute();
  } catch(Exception $e) {
    echo "Unable to retrieve results";
    exit;
  }
  $catalog = $results->fetchAll();
  return $catalog;
}

function random_catalog_array() {
  include("connection.php");
  try {
      $results = $db->query(
        "SELECT media_id, title, category, img 
        FROM Media
        ORDER BY RANDOM()
        LIMIT 4"
      );
  } catch(Exception $e) {
    echo "Unable to retrieve results";
    exit;
  }
  $catalog = $results->fetchAll();
  return $catalog;
}

function single_item_array($id) {
  include("connection.php");
  try {
      $results = $db->prepare(
     "SELECT title, category, img, format, year, genre, publisher, isbn 
      FROM Media
      JOIN Genres ON Media.genre_id = Genres.genre_id
      LEFT OUTER JOIN Books ON Media.media_id = Books.media_id
      WHERE Media.media_id = ?"
                           );
    $results->bindParam(1, $id, PDO::PARAM_INT);
    $results->execute();
  } catch(Exception $e) {
    echo "Unable to retrieve results";
    exit;
  }
  $item = $results->fetch();
  if (empty($item)) return $item;
  try {
      $results = $db->prepare(
     "SELECT fullname, role
      FROM Media_People
      JOIN People ON Media_People.people_id = People.people_id
      WHERE Media_People.media_id = ?"
                           );
    $results->bindParam(1, $id, PDO::PARAM_INT);
    $results->execute();
  } catch(Exception $e) {
    echo "Unable to retrieve results";
    exit;
  }
  while($row = $results ->fetch(PDO::FETCH_ASSOC)) {
    $item[$row["role"]][] = $row["fullname"];
  }
  return $item;
}

function get_item_html($item) {
    $output = "<li><a href='details.php?id="
        . $item["media_id"] . "'><img src='" 
        . $item["img"] . "' alt='" 
        . $item["title"] . "' />" 
        . "<p>View Details</p>"
        . "</a></li>";
    return $output;
}

function array_category($catalog,$category) {
    $output = array();

    foreach ($catalog as $id => $item) {
        if ($category == null OR strtolower($category) == strtolower($item["category"])) {
            $sort = $item["title"];
            $sort = ltrim($sort,"The ");
            $sort = ltrim($sort,"A ");
            $sort = ltrim($sort,"An ");
            $output[$id] = $sort;            
        }
    }

    asort($output);
    return array_keys($output);
}

3 Answers

Hi Zachary,

I think it would be line 46 you have your else statement. There's a special character at the end of that line right after the left curly brace that you should try deleting. I think it's the delete character, code 0x7f

That might be causing your syntax error. You might not be able to see it in workspaces but if you paste that part of your code into a text editor then you should be able to see it. I see it as a box in both notepad++ and sublime text.

See the comment in the code

if (is_integer($limit)) {
      $results = $db->prepare($sql. " LIMIT ? OFFSET ?");
      $results->bindParam(1,$limit,PDO::PARAM_INT);
      $results->bindParam(2,$offset,PDO::PARAM_INT);
    } else {  // this is line 46. There's a special character here after the left curly brace that you should delete.
      $results = $db->prepare($sql);
      }        
      $results->execute();

If that doesn't solve your problem you can post a snapshot of your workspace and that will make it easier to troubleshoot.

Did you try deleting the character that I mentioned in my answer?

I forked your workspace and deleted that character and it seems to be rendering fine now.

You have to go to the end of line 46, and then hit the backspace key one time. It won't delete the curly brace but it will delete that special character you have.

I'm not sure how you got it in there though.

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

Thanks Jason! That solved everything and everything is now running. I really appreciate the help!