chris17 Posted November 7, 2013 Share Posted November 7, 2013 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/283696-please-i-need-help-with-this-function/ Share on other sites More sharing options...
Solution mac_gyver Posted November 7, 2013 Solution Share Posted November 7, 2013 (edited) 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 November 7, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/283696-please-i-need-help-with-this-function/#findComment-1457419 Share on other sites More sharing options...
chris17 Posted November 7, 2013 Author Share Posted November 7, 2013 Thanks Boss, it was perfect. Quote Link to comment https://forums.phpfreaks.com/topic/283696-please-i-need-help-with-this-function/#findComment-1457450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.