我的SQL语法有错误

时间:2013-05-14 13:24:09

标签: php mysql

下面的php代码执行搜索后会显示以下消息。我试图检查错误,但我没有找到任何有用的东西。怎么做?问题是什么?感谢

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近' field4 LIKE'%aaa%' ORDER BY filed1,field2,field3,field4'在线   1

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);
}
?>

4 个答案:

答案 0 :(得分:4)

callMySQL reserved word。您需要在ticks中包装以保留字命名的任何列。

答案 1 :(得分:2)

例如CALLreserved word in mysql,因此您需要使用反引号来转义它:

`call`

虽然没有看到你的sql,但很难确切地告诉你。

答案 2 :(得分:0)

试试这个:

"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"

答案 3 :(得分:0)

由于错误在“MySQL语法”中,请关注查询。这部分代码:

$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";

似乎是您的查询,但我没有看到错误中出现的“在哪里调用......”。

要确定问题,我会在运行查询之前添加此行:

echo $query."<br />";

然后您可以看到实际导致错误的查询。如果使用其他查询,请执行相同操作。找出哪一个确实导致了问题。