在我的搜索脚本中,我遇到以下代码的结果时遇到了麻烦:
$sqlCommand = "SELECT `Loc.`, Model, Make, Description, `Tag No.`,
MATCH(`Loc.`, Model, Make, Description, `Tag No.`)
AGAINST ('$searchquery' IN BOOLEAN MODE) AS score FROM ag_combine
WHERE MATCH(`Loc.`, Model, Make, Description, `Tag No.`)
AGAINST ('$searchquery' IN BOOLEAN MODE) AND (Status='IN' or Status='OF') ORDER BY score DESC";
在搜索列到Tag No.
列之前,查询工作正常,然后它会显示0结果消息;但是phpMyAdmin中的相同查询将产生所需的标签号结果。
我猜它与列名中的空格有关,但不确定如何从中正确选择。
以下是我正在测试的完整代码:
<?php
include "includes/config.php";
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!+-]#i', '', $_POST['searchquery']);
if($_POST['filter1'] == "Combines"){
$sqlCommand = "SELECT `Loc.`, Model, Make, Description, `Tag No.`,
MATCH(`Loc.`, Model, Make, Description, `Tag No.`)
AGAINST ('$searchquery' IN BOOLEAN MODE) AS score FROM ag_combine
WHERE MATCH(`Loc.`, Model, Make, Description, `Tag No.`)
AGAINST ('$searchquery' IN BOOLEAN MODE) AND (Status='IN' or Status='OF') ORDER BY score DESC";
} else if($_POST['filter1'] == "Tractors"){
$sqlCommand = "(SELECT `Loc.`, Model, Make, Description, Status, `Tag No.` FROM ag_tractor WHERE MATCH (`Loc.`, Model, Make, Description,`Tag No.` ) AGAINST ('$searchquery' IN BOOLEAN MODE) AND (Status='IN' or Status='OF'))";
}
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysql_fetch_array($query)){
$loc = $row["Loc."];
$tag = $row["Tag No."];
$model = $row["Model"];
$make = $row["Make"];
$description = $row["Description"];
$search_output .= "$loc: $tag: $make $model - $description<br />";
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For:
<input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="Combines">Combines</option>
<option value="Tractors">Tractors</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>