Jump to content

Please I need help with this function


chris17

Recommended Posts

Please am a newbie to php. When ever this block is executed, it returns an SQL syntax error as follows "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 '= 1 LIMIT 1' at line 1". Please help me am stuck.

 

function get_subject_by_id($subject_id){

    $query = "SELECT * ";
    $query .= "FROM subjects";
    $query .= "WHERE id = {$subject_id} ";
    $query .= "LIMIT 1";

    $result_set = mysql_query($query);
    confirm_query($result_set);

    if ($subject = mysql_fetch_array($result_set)){
        return $subject;
        } else {
            return NULL;
            }
    }

 

//or

 

function get_page_by_id($page_id){
    
    $query = "SELECT * ";
    $query .= "FROM pages";
    $query .= "WHERE id = {$page_id} " ;
    $query .= "LIMIT 1";

    $result_set = mysql_query($query);
    confirm_query($result_set);
    
    if ($page = mysql_fetch_array($result_set)){
        return $page;
        } else {
            return NULL;
            }
    }
 

Link to comment
https://forums.phpfreaks.com/topic/283696-please-i-need-help-with-this-function/
Share on other sites

you don't have any white-space between the table name and the WHERE keyword (probably in both queries.) you need to build your query statement using one php statement so that you have minimum of different contexts -

$query = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1";

or if you want some formatting (for longer query statements)  -

$query = "SELECT * FROM subjects
    WHERE id = {$subject_id}
    LIMIT 1";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.