Database Query Failed: right syntax to use near 'LIMIT 1'

Discussion in 'PHP' started by Sacramentum, May 29, 2011.

  1. #1
    Hey All.

    I am a MySQL newb, and wasn't sure whether to put this in the newbie forum, however the question is from PHP, so I figured to be on the safe side, I would post in here.

    Here's the drama in a nutshell. I am following the Lynda.com PHP & MySQL training (*ducks*) and have run into an error I can't seem to resolve myself. As I am a newb to PHP, MySQL I have tried troubleshooting this myself, but I think I need some help.

    I am running:
    Windows 7
    Testing on XAMP 1.7.3


    Issue is as follows: This particular line seems to be the culprit:

    function get_subject_by_id($subject_id) {
    global $connection;
    $query = "SELECT * ";
    $query .= "FROM subjects ";
    $query .= "WHERE id=" . $subject_id ." ";
    $query .= "LIMIT 1";
    $result_set = mysql_query($query, $connection);
    confirm_query($result_set);
    // REMEMBER:
    // if no rows are returned, fetch_array will return false
    if ($subject = mysql_fetch_array($result_set)) {
    return $subject;
    } else {
    return NULL;
    }
    }


    Some explanation:
    -$connection is a global reference to my connection to the MySQL server.
    -this function is from an external function file that is included in the document.
    -confirm_query is a function that tests the the mysql_query and echo's the error back to the browser if it fails. This is where I am getting my error messages from.

    The error returned in the browser is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

    I have used my code and the instructors code (that works on the video *of course*) and get the same message on both of my machines. Some troubleshooting I have attempted:

    -Instructor said the "LIMIT 1" line is optional... eliminated it and recieved this error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    -rebuilt the SQL query to "look" more normal:

    function get_subject_by_id($subject_id) {
    global $connection;
    $query = "SELECT * FROM subjects WHERE id=" . $subject_id ." LIMIT 1";
    $result_set = mysql_query($query, $connection);
    confirm_query($result_set);
    // REMEMBER:
    // if no rows are returned, fetch_array will return false
    if ($subject = mysql_fetch_array($result_set)) {
    return $subject;
    } else {
    return NULL;
    }
    }
    -again, eliminated LIMIT 1, issue persists
    -put the subject ID inline:

    function get_subject_by_id($subject_id) {
    global $connection;
    $query = "SELECT * FROM subjects WHERE id={$subject_id} LIMIT 1";
    $result_set = mysql_query($query, $connection);
    confirm_query($result_set);
    // REMEMBER:
    // if no rows are returned, fetch_array will return false
    if ($subject = mysql_fetch_array($result_set)) {
    return $subject;
    } else {
    return NULL;
    }
    }
    -again eliminated LIMIT 1, issue persists.

    If I can provide any more info, please let me know! And let me say, THANKS ahead of time for the help!
     
    Sacramentum, May 29, 2011 IP
  2. dazst

    dazst Active Member

    Messages:
    115
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #2
    $query = "SELECT * FROM subjects WHERE id=\"$subject_id\" LIMIT 1";
     
    dazst, May 29, 2011 IP
  3. piluan

    piluan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $query .= "LIMIT 0,1";
     
    piluan, May 30, 2011 IP