jobs1109 Posted July 27, 2011 Share Posted July 27, 2011 Hi, I am getting an error message from my mysql query I am using two variables passed from another page that I use in the query. $Keyword_Search $Jobcategory when I echo out the variables on this page it show the values entered but how do I used it in my query. Here is the code. <?php include'../somedatabasefilehere'; $Keyword_Search = $_SESSION['Keyword_Search']; $Jobcategory = $_SESSION['Jobcategory']; echo $Keyword_Search; echo $Jobcategory; // getting data from table "Table" $result = mysql_query(SELECT * FROM Tablename WHERE Keyword_search = "$Keyword_Search" and Jobcategory="$Jobcategory"'); while($row = mysql_fetch_array($result)) { echo $row['Keyword_Search'] . " " . $row['Jobcategory']; echo "<br />"; } ?> Here is the error message I am getting Parse error: syntax error, unexpected T_STRING in /home/content/h/i/l/hilo101/html/Hilo-Jobs/Job-Search-Form/Getting-Data-From-Database/Getting-Data-Database.php on line 41 Please help. Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/ Share on other sites More sharing options...
AyKay47 Posted July 27, 2011 Share Posted July 27, 2011 you havn't wrapped your query in quotes $result = mysql_query("SELECT * FROM Tablename WHERE Keyword_search = '$Keyword_Search' and Jobcategory='$Jobcategory'"); Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248096 Share on other sites More sharing options...
jobs1109 Posted July 27, 2011 Author Share Posted July 27, 2011 Still getting error message Here is the full code <?php session_start(); ?> <?php $_SESSION['Keyword_Search'] = $_POST['Keyword_Search']; $_SESSION['Jobcategory'] = $_POST['Jobcategory']; $Keyword_Search = $_SESSION['Keyword_Search']; $Jobcategory = $_SESSION['Jobcategory']; echo $Keyword_Search; echo $Jobcategory; ?> <?php include'Some-database-connect-file'; // getting data from table "Sometable" $result = mysql_query("SELECT * FROM sometable WHERE Keyword_search = '$Keyword_Search' and Jobcategory='$Jobcategory'"); while($row = mysql_fetch_array($result)) { echo $row['Keyword_Search'] . " " . $row['Jobcategory']; echo "<br />"; } ?> Here is the error message I am getting. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/h/i/l/hilo101/html/Hilo-Jobs/Job-Search-Form/Getting-Data-From-Database/Getting-Data-Database.php on line 33 Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248108 Share on other sites More sharing options...
AyKay47 Posted July 27, 2011 Share Posted July 27, 2011 the error is indicating that your query is not correct and is returning false.. 1. make sure that you are connected to your db server.. 2. use mysql_error to debug your query 3. echo your query to make sure that it is what you want it to be Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248116 Share on other sites More sharing options...
fenway Posted July 27, 2011 Share Posted July 27, 2011 Definitely do #3. Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248198 Share on other sites More sharing options...
silkfire Posted July 28, 2011 Share Posted July 28, 2011 Check if your $_POST variables contains apostrophes. Those strings must be escaped prior to be used in a query by using mysql_real_escape_string() Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248769 Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 Check if your $_POST variables contains apostrophes. Those strings must be escaped prior to be used in a query by using mysql_real_escape_string() even if they aren't, that wouldn't cause errors... Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248774 Share on other sites More sharing options...
silkfire Posted July 29, 2011 Share Posted July 29, 2011 Yes it would, Mysql would complain that the query was unexpectedly cut off in the middle. You should look for bugs in all possible causes. Quote Link to comment https://forums.phpfreaks.com/topic/243003-mysql-query/#findComment-1248887 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.