In my search script I'm having troubles getting a result for the following code:
$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";
The query works fine when searching columns up until the Tag No.
column, then it'll show a 0 Results message; however the same query in phpMyAdmin will produce the desired Tag No. result.
I'm guessing it has to do with the blank space in the column name, but not sure how to correctly select from it.
Here is the full code I'm testing:
<?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>