mikebyrne Posted April 2, 2008 Share Posted April 2, 2008 Im getting the error and cant spot my mistake error in the queryYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%'' at line 1 <?PHP include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CD</option> <option value="dvd" >DVD</option> <option value="game" >Game</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/ Share on other sites More sharing options...
mkoga Posted April 2, 2008 Share Posted April 2, 2008 try echoing out the value of $query to get a better idea of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507221 Share on other sites More sharing options...
mikebyrne Posted April 2, 2008 Author Share Posted April 2, 2008 Would that be echo "['$query']";? Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507226 Share on other sites More sharing options...
mikebyrne Posted April 2, 2008 Author Share Posted April 2, 2008 Ive tried this but it doesnt seem to be showing the varible but the page wont load so the problem seems to be with the line $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; <?PHP include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); echo $query; if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CDs</option> <option value="dvd" >DVDs</option> <option value="game" >Games</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> </table> Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507235 Share on other sites More sharing options...
moon 111 Posted April 2, 2008 Share Posted April 2, 2008 Check if its empty <?php if(empty($query)) echo "Empty!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507236 Share on other sites More sharing options...
mikebyrne Posted April 2, 2008 Author Share Posted April 2, 2008 Well it would be empty because the page wont load it just give me the error messege Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507241 Share on other sites More sharing options...
Catfish Posted April 2, 2008 Share Posted April 2, 2008 Try changing $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; to $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%".$input."%'"; if that doesn't work I would be monitoring the value of $input through your script to see what value is passed into the $query string. Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507272 Share on other sites More sharing options...
thenature4u Posted April 2, 2008 Share Posted April 2, 2008 while($row = mysql_fetch_array) { change this to while($row = mysql_fetch_array($result)) { Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507275 Share on other sites More sharing options...
mkoga Posted April 2, 2008 Share Posted April 2, 2008 Would that be echo "['$query']";? no just echo $query. Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507769 Share on other sites More sharing options...
mikebyrne Posted April 2, 2008 Author Share Posted April 2, 2008 No, still getting the same error and tried Everything!!!! I can pm people my complete code if you'd like to take a look? Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507873 Share on other sites More sharing options...
mikebyrne Posted April 2, 2008 Author Share Posted April 2, 2008 It has to be something to do with the placment of the php rather than the actual code? Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507941 Share on other sites More sharing options...
AndyB Posted April 2, 2008 Share Posted April 2, 2008 <form name="ex2" method="get" action="searchresults.php"> $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); Since the form method is GET there aren't going to be any POST variables. Your form is also mangled. The select option needs to be inside the form tags. The onChange serves no purpose then. Quote Link to comment https://forums.phpfreaks.com/topic/99136-error-in-your-sql-syntax/#findComment-507968 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.