由下面的 php 代码执行搜索后,将显示以下消息。我试图检查错误,但没有发现任何有用的东西。怎么做?有什么问题?谢谢
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'field4 LIKE '%aaa%' ORDER BY filed1, field2, field3, field4' 附近使用正确的语法
php代码是:
<?php
//Get variables from config.php to connect to mysql server
require 'config.php';
// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the 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 from variable
$search = trim($search);
$search = preg_replace('/\s+/', ' ', $search);
//seperate multiple keywords into array space delimited
$keywords = explode(" ", $search);
//Clean empty arrays so they don't get every row as result
$keywords = array_diff($keywords, array(""));
//Set the MySQL query
if ($search == NULL or $search == '%'){
} else {
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM mytable " .
"WHERE field1 LIKE '%".$keywords[$i]."%'".
" OR field2 LIKE '%".$keywords[$i]."%'" .
" OR field3 LIKE '%".$keywords[$i]."%'" .
" OR field4 LIKE '%".$keywords[$i]."%'" .
" ORDER BY field1, field2, field3, field4";
}
//Store the results in a variable or die if query fails
$result = mysql_query($query) or die(mysql_error());
}
if ($search == NULL or $search == '%'){
} else {
//Count the rows retrived
$count = mysql_num_rows($result);
}
echo "<html>";
echo "<head>";
echo "<titleTitle of the 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 is null do nothing, else print it.
if ($search == NULL) {
} else {
echo "Searched <b><FONT COLOR=\"blue\">";
foreach($keywords as $value) {
print "$value ";
}
echo "</font></b>";
}
echo "<p> </p><br />";
echo "</center>";
//If users doesn't enter anything into search box tell them to.
if ($search == NULL){
echo "<center><b><FONT COLOR=\"red\">Please insert a key to search</font></b><br /></center>";
} elseif ($search == '%'){
echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>";
//If no results are returned print it
} elseif ($count <= 0){
echo "<center><b><FONT COLOR=\"red\">No result found</font></b><br /></center>";
//ELSE print the data in a 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 for alternation of row color on results table
$color1 = "#C1D6F0";
$color2 = "#C1D6F0";
//While there are rows, print it.
while($row = mysql_fetch_array($result))
{
//Row color alternates for 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);
}
?>