maazzarif Posted February 17, 2013 Share Posted February 17, 2013 SQL QUERY PROBLEM IN PHP. I m using windows xp O.S. I have installed WAMP for learning and practicing PHP. when I execute the following code i m getting an error.I M INCLUDING A FUNCTION FILE(functions.php) IN MY content.php file.MY ALL OTHER FUNCTIONS AND SQL QUERIES ARE WORKING EXCEPT THIS ONE. ---------------------------------------------------------------------------------------------------------------- functions.php(c:/wamp/www/widget_corp/includes/functions.php) function get_subject_by_id($subject_id){ global $connection; $query = "SELECT * "; $query .= "FROM subjects "; $query .= "WHERE id=".$subject_id; $query .= "LIMIT 1"; $result_set = mysql_query($query,$connection); if(!$result_set){ die("database query failed:".mysql_error()); } //if no rows are returned, then fetch array is going to return false if($subject = mysql_fetch_array($result_set)) { return $subject; } else { return NULL; } } ----------------------------------------------------------------------------------------------------------------- content.php(c:/wamp/www/widget_corp/content.php) <?php require("constants.php"); //Creating a database connection $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD); if(!$connection){ die("DATABASE CONNECTION FAILED:".mysql_error()); } ?> <?php //Selecting a database $db_connect = mysql_select_db(DB_NAME,$connection); if(!$db_connect){ die("Database selection failed:".mysql_error()); } ?> <?php //including functions.php ?> <?php require_once($_SERVER['DOCUMENT_ROOT']."/widget_corp/includes/functions.php") ?> <?php //$_GET['subj'] and $_GET['page'] are returning my subjects and pages ids respectively. ?> <?php if(isset($_GET['subj'])){ $sel_subj = $_GET['subj']; $sel_pg=""; }elseif(isset($_GET['page'])){ $sel_pg= $_GET['page']; $sel_subj = ""; } else { $sel_subj = ""; $sel_pg = ""; } $sel_subject = get_subject_by_id($sel_subj); $sel_page = get_page_by_id($sel_pg); ?> ------------------------------------------------------------------------------------------------------------------ ERROR(ON BROWSER): 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 'LIMIT 1' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/ Share on other sites More sharing options...
denno020 Posted February 17, 2013 Share Posted February 17, 2013 I think you might need to add quotes around subject_id so like this: $query .= "WHERE id=`$subject_id`"; Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1412909 Share on other sites More sharing options...
maazzarif Posted February 17, 2013 Author Share Posted February 17, 2013 (edited) @ denno020 I think it will make $subject_id as a string.Well i tried it and i got just a slight change in the error. Following is the error i got: 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' at line 1 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' at line 1 Edited February 17, 2013 by maazzarif Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1412911 Share on other sites More sharing options...
Barand Posted February 17, 2013 Share Posted February 17, 2013 (edited) @denno, you should not quote number values. @mazzarrif, echo $query to see what it looks like. I think you may have a spacing issue. edit. Create query code with multiple lines $query = "SELECT * FROM subjects WHERE id = $subject_id LIMIT 1"; Not only is its structure easier to read but when you get a syntax error it tells you which line of the query. This way everything is not on line 1. Edited February 17, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1412914 Share on other sites More sharing options...
maazzarif Posted February 17, 2013 Author Share Posted February 17, 2013 (edited) @denno, you should not quote number values. @mazzarrif, echo $query to see what it looks like. I think you may have a spacing issue. edit. Create query code with multiple lines $query = "SELECT * FROM subjects WHERE id = $subject_id LIMIT 1"; Not only is its structure easier to read but when you get a syntax error it tells you which line of the query. This way everything is not on line 1. Sir i did as you said. and this is what i got 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 'LIMIT 1' at line 4 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 'LIMIT 1' at line 4 Edited February 17, 2013 by maazzarif Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1412915 Share on other sites More sharing options...
Barand Posted February 17, 2013 Share Posted February 17, 2013 echo $query; ? Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1412920 Share on other sites More sharing options...
j0nnie_m Posted September 13, 2013 Share Posted September 13, 2013 The error: "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 'LIMIT 1' at line 1" Can Be fixed, try this: change $query .= "LIMIT 1"; to $query .= "LIMIT 0,1";and $query .= "WHERE id=" . $subject_id ." "; to $query .= "WHERE id=" . (int)$subject_id ." "; regards! Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1449331 Share on other sites More sharing options...
Barand Posted September 13, 2013 Share Posted September 13, 2013 j0nnie, LIMIT 1 and LIMIT 0,1 are identical. It is more likely that $subject_id has no value but as the OP refuses to echo $query to verify then who knows. My view is that when more info (like echo $query) is requested but the OP can't be bothered then the only thing to do is walk away. Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1449402 Share on other sites More sharing options...
priyankagound Posted September 16, 2013 Share Posted September 16, 2013 Best to echo the query and see what it looks like. Probably $subject_id contains no value or an invalid value. If $subject_id is a string, you should escape it (using mysql_real_escape_string) and put it inside quotes in the query. [Edit] You know you can put enters in strings too, right? // More readable$query = "SELECT *FROM subjectsWHERE id = $subject_idLIMIT 1"; Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1449681 Share on other sites More sharing options...
cyberRobot Posted September 16, 2013 Share Posted September 16, 2013 There needs to be a space between the WHERE and LIMIT clauses. <?php $query .= "WHERE id=$subject_id "; //<-- added a space here $query .= "LIMIT 1"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/274589-sql-query-error-in-php/#findComment-1449695 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.