php - I have an error in your SQL syntax -


the following message displayed after search performed php code below. tried check mistake didn't find of useful. how do? what's problem? thanks

you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'field4 '%aaa%' order filed1, field2, field3, field4' @ line 1

the php code is:

<?php   //get variables config.php connect mysql server require 'config.php';  // connect mysql database server. mysql_connect ($dbhost, $dbusername, $dbuserpass); //select database mysql_select_db($dbname) or die('cannot select database');  //search variable = data in search box or url if(isset($_get['search'])) { $search = $_get['search']; }  //trim whitespace variable $search = trim($search); $search = preg_replace('/\s+/', ' ', $search);  //seperate multiple keywords array space delimited $keywords = explode(" ", $search);  //clean empty arrays don't every row result $keywords = array_diff($keywords, array(""));  //set mysql query if ($search == null or $search == '%'){ } else { ($i=0; $i<count($keywords); $i++) { $query = "select * mytable " . "where field1 '%".$keywords[$i]."%'". " or field2 '%".$keywords[$i]."%'" . " or field3 '%".$keywords[$i]."%'" . " or field4 '%".$keywords[$i]."%'" . " order field1, field2, field3, field4"; }  //store results in variable or die if query fails $result = mysql_query($query) or die(mysql_error()); } if ($search == null or $search == '%'){ } else { //count rows retrived $count = mysql_num_rows($result); }  echo "<html>"; echo "<head>"; echo "<titletitle of page</title>"; echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"; echo "</head>"; echo "<body onload=\"self.focus();document.searchform.search.focus()\">"; echo "<center>"; echo "<br /><form name=\"searchform\" method=\"get\" action=\"search.php\">"; echo "<input type=\"text\" name=\"search\" size=\"20\" tabindex=\"1\" />"; echo " <input type=\"submit\" value=\"cerca\" />"; echo "</form>"; //if search variable null nothing, else print it. if ($search == null) { } else { echo "searched <b><font color=\"blue\">"; foreach($keywords $value) {    print "$value "; } echo "</font></b>"; } echo "<p> </p><br />"; echo "</center>";  //if users doesn't enter search box tell them to. if ($search == null){ echo "<center><b><font color=\"red\">please insert key search</font></b><br /></center>"; } elseif ($search == '%'){ echo "<center><b><font color=\"red\">please enter search parameter continue.</font></b><br /></center>"; //if no results returned print } elseif ($count <= 0){ echo "<center><b><font color=\"red\">no result found</font></b><br /></center>"; //else print data in table } else { //table header echo "<center><table style=\"text-align: left; margin-left: auto; margin-right: auto;  border=\"1\" bordercolor cellspacing=\"1\" cellpadding=\"4\" cols=\"4\" frame=\"border\" rules=\"none\">"; echo "<tbody>"; echo "<thead><tr>"; echo "<td style=\"sdnum=\"1040;1040;standard\" align=\"center\" bgcolor=\"#0049a3\" height=\"25\" valign=\"middle\" ><b><font color=\"#ffffff\" size=\"3\">field1</span></td>"; echo "<td style=\"sdnum=\"1040;1040;standard\" align=\"center\" bgcolor=\"#0049a3\"  height=\"25\"valign=\"middle\"><b><font color=\"#ffffff\" size=\"3\">field2</span></td>"; echo "<td style=\"sdnum=\"1040;1040;standard\" align=\"center\" bgcolor=\"#0049a3\" height=\"25\"valign=\"middle\"><b><font color=\"#ffffff\" size=\"3\">field3</span></td>"; echo "<td style=\"sdnum=\"1040;1040;standard\" align=\"center\" bgcolor=\"#0049a3\" height=\"25\"valign=\"middle\"><b><font color=\"#ffffff\" size=\"3\">field4</span></td>"; echo "</tr></thead><tbody>";   //colors alternation of row color on results table $color1 = "#c1d6f0"; $color2 = "#c1d6f0"; //while there rows, print it. while($row = mysql_fetch_array($result)) { //row color alternates each row $row_color = ($row_count % 2) ? $color1 : $color2; //table background color = row_color variable  echo "<td bgcolor=\"c1d6f0\" align=center valign=middle sdnum=\"1040;1040;standard\" ><font size=2 color=\"#000000\">".$row['field1']."</td>"; echo "<td bgcolor=\"c1d6f0\" align=left valign=middle sdnum=\"1040;1040;standard\"><font size=2 color=\"#000000\">".$row['field2']."</td>"; echo "<td bgcolor=\"c1d6f0\" align=left valign=middle sdnum=\"1040;1040;standard\"><font size=2 color=\"#000000\">".$row['field3']."</td>"; echo "<td bgcolor=\"c1d6f0\" align=center valign=middle sdnum=\"1040;1040;standard\"><font size=2 color=\"#000000\">".$row['field4']."</td>"; echo "</tr>";    $row_count++; //end while } //end if } echo "</table></center>";  echo "</body>"; echo "</html>"; if ($search == null or $search == '%') { } else { //clear memory mysql_free_result($result); } ?> 

for example call reserved word in mysql, need escape using backticks:

`call` 

hard tell without seeing sql though.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -