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

Why won't this php database call work?

I am building a little practice project and I am trying to incorporate pagination of a set of items, so I have limit and offset values that are passed to a function, and those values are set dynamically. I have already checked several times and the values I am trying to pass to the function are correct, but still the function doesn't work. It is hitting the exception block and exiting. Here is the function:

function retrieve_saved_notes_list($userid, $limit, $offset) {
  include("inc/database_connection.php");
  try {
    $sql = "SELECT * FROM notes
            WHERE user_id = ?
            LIMIT = ?
            OFFSET = ?";
    $results = $db->prepare($sql);
    $results->bindParam(1, $userid, PDO::PARAM_INT);
    $results->bindParam(2, $limit, PDO::PARAM_INT);
    $results->bindParam(3, $offset, PDO::PARAM_INT);
    $results->execute();
  } catch (Exception $e) {
    $e->getMessage();
    exit;
  }
  $results = $results->fetchAll();
  return $results;
}

Is there something wrong with this function that I am not seeing?

UPDATE: when I echo the error message this is what I get:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= 10 OFFSET = 0' at line 3

No idea what is wrong with the syntax. The syntax looks fine to me :s.

1 Answer

Try without the equals sign for limit and offset. "SELECT * FROM notes WHERE user_id = ? LIMIT = 10 OFFSET = 0" isn't valid SQL syntax whereas "SELECT * FROM notes WHERE user_id = ? LIMIT 10 OFFSET 0" is valid.

Thanks. Can't believe I missed that!