Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP SQL database query error message [duplicate]

Tags:

database

php

Is there anything wrong with this SQL code? I got it from a tutorial, but it's returning the following error message

Database query failed: 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

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);
    // if no rows are returned, fetch array will return false
    if ($subject = mysql_fetch_array($result_set)){
    return $subject;
    } else {
    return NULL;

    }
    }
like image 515
Leahcim Avatar asked May 23 '26 00:05

Leahcim


1 Answers

Best to echo the query and see what it looks like.

Probably $subject_id contains no value or an invalid value. If $subject_id is a string, you should escape it (using mysql_real_escape_string) and put it inside quotes in the query.

[Edit]

You know you can put enters in strings too, right?

// More readable
$query = "
  SELECT *
  FROM subjects
  WHERE id = $subject_id
  LIMIT 1";
like image 117
GolezTrol Avatar answered May 24 '26 15:05

GolezTrol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!