Jump to content

Please I need help with this function


chris17
Go to solution Solved by mac_gyver,

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
Share on other sites

  • Solution

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";
Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.