phpnewbe Posted October 21, 2008 Share Posted October 21, 2008 I keep on getting this error message on my function: 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. The function itself is: function get_page_by_id($page_id) { global $connection; $query = "SELECT * "; $query .= "FROM pages "; $query .= "WHERE id=" . $page_id . " "; $query .= "LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if ($subject = mysql_fetch_array($result_set)) { return $subject; } else {return NULL;} } My question is : where exactly is this mistake with SQL then? If I remove "LIMIT 1" I still get the message that there's something wrong with query. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 21, 2008 Share Posted October 21, 2008 Just echo $query to see what is in it. $page_id is probably empty. Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-671222 Share on other sites More sharing options...
phpnewbe Posted October 22, 2008 Author Share Posted October 22, 2008 Just echo $query to see what is in it. $page_id is probably empty. You right it's empty. And I don't know why. This is how it is being set: <?php if (isset($_GET['subj'])) { $sel_subj = $_GET['subj']; $sel_page = ""; } elseif (isset($_GET['page'])) { $sel_subj = ""; $sel_page = $_GET['page']; } else { $sel_subj = ""; $sel_page = ""; } $sel_subject = get_subject_by_id($sel_subj); ?> $sel_sub is passed to here 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 no rows are returned fetch_array will return false. if ($subject = mysql_fetch_array($result_set)) { return $subject; } else { return NULL;} } but it cannot be empty! Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-671833 Share on other sites More sharing options...
Maq Posted October 22, 2008 Share Posted October 22, 2008 You right it's empty. And I don't know why. This is how it is being set: Uh, where is $page_id being set? Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-671838 Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2008 Share Posted October 22, 2008 You changed which code you are talking about. Anyway, is the parameter a string or a number? If it is a string (the code allows/sets an empty "" for a value), then it must be enclosed in single quotes in the query. Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-671857 Share on other sites More sharing options...
phpnewbe Posted October 22, 2008 Author Share Posted October 22, 2008 You changed which code you are talking about. Anyway, is the parameter a string or a number? If it is a string (the code allows/sets an empty "" for a value), then it must be enclosed in single quotes in the query. It is integer. Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-671934 Share on other sites More sharing options...
dilum Posted October 23, 2008 Share Posted October 23, 2008 Try this, function get_page_by_id($page_id) { global $connection; $query = "SELECT * "; $query .= "FROM pages "; $query .= "WHERE id= ' " . $page_id . " ' "; $query .= "LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if ($subject = mysql_fetch_array($result_set)) { return $subject; } else {return NULL;} } *put where id = '' like this.... Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-672750 Share on other sites More sharing options...
phpnewbe Posted October 23, 2008 Author Share Posted October 23, 2008 Try this, function get_page_by_id($page_id) { global $connection; $query = "SELECT * "; $query .= "FROM pages "; $query .= "WHERE id= ' " . $page_id . " ' "; $query .= "LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if ($subject = mysql_fetch_array($result_set)) { return $subject; } else {return NULL;} } *put where id = '' like this.... I changed it like this: function get_subject_by_id($subject_id) { global $connection; $query = "SELECT * "; $query .= "FROM pages "; $query .= "WHERE id= ' " . $subject_id . " ' "; $query .= "LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); if ($subject = mysql_fetch_array($result_set)) { return $subject; } else {return NULL;} } Because here <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php if (isset($_GET['subj'])) { $sel_subj = $_GET['subj']; $sel_page = ""; } elseif (isset($_GET['page'])) { $sel_subj = ""; $sel_page = $_GET['page']; } else { $sel_subj = ""; $sel_page = ""; } $sel_subject = get_subject_by_id($sel_subj); ?> this function puts there $sel_subj. But I still get "Unknown column 'subject_id' in 'where clause'" error. Which doesn't make sense, because $sel_subj is not empty - it always has integer subject id in it. Maybe I should try to send you the whole code if that would make it easier to track? Quote Link to comment https://forums.phpfreaks.com/topic/129473-can-anybody-help-me-with-this-query-function/#findComment-672971 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.