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; } } Link to comment https://forums.phpfreaks.com/topic/283696-please-i-need-help-with-this-function/ Share on other sites More sharing options...
mac_gyver Posted November 7, 2013 Share Posted November 7, 2013 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"; 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. 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
Archived
This topic is now archived and is closed to further replies.