0

为什么当我尝试搜索 mysql 数据库时,它显示 0 结果?我正确地将表格更改为 FULLTEXT 等。我错过了什么?我在这里放了非常基本的搜索练习代码。我想搜索我的表“产品”并从那里提取详细信息。但没有运气

<?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'] == "Whole Site"){
        $sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ('$searchquery' IN BOOLEAN MODE)";
    } 
    require_once("storescripts/connect_to_mysqli.php");
    $query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
    $count = mysqli_num_rows($query);
    if($count > 1){
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
        while($row = mysqli_fetch_array($query)){

            $name = $row["name"];
                    $details= $row["details"];
            $search_output .= "Name: '.$name.' - '.$details.'<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="Whole Site">Whole Site</option>

</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>
4

2 回答 2

3

改变这个:

$sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ('".$searchquery."' IN BOOLEAN MODE)";

我改变了

AGAINST ('$searchquery' IN

AGAINST ('".$searchquery."' IN 
于 2013-08-12T09:17:17.490 回答
0

您的 if 部分存在问题:

  1. 替换您当前的:

    if($_POST['filter1'] == "Whole Site") { $sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ({$searchquery} IN BOOLEAN MODE)"; }

  2. 如果由于 if 语句而无法查询,请添加:

    else { echo "No seqrch query found"; }

希望它现在有效

于 2013-08-12T09:34:05.273 回答