dpjpo746884 2013-08-12 10:30
浏览 44
已采纳

Php Search Script只选取第一个单词

As the question, it only picked up the first word in my product_name which has var_char(50) as primary key.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = $_POST['searchquery'];
    if($_POST['filter1'] == "Whole Site"){
    $sqlCommand = "SELECT * FROM products WHERE MATCH (product_name ,details,category,subcategory) 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)){
                $id=$row["id"];
            $product_name = $row["product_name"];
                    $details= $row["details"];
                $category=$row["category"];
                $subcategory=$row["subcategory"];
            $search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><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>

is this due to that it is a primary key? It actually not working for details as well, it actually very weird, not working for most words but work for some..i am totally confused..it is not making any senses

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once("storescripts/connect_to_mysqli.php");
$search_output = "";

if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = $_POST['searchquery'];
    if($_POST['filter1'] == "Whole Site"){
$sqlCommand = sprintf("SELECT * FROM products WHERE product_name LIKE '%s%%'",
               mysqli_real_escape_string($myConnection, $searchquery));
    } 

    $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)){
                $id=$row["id"];
            $product_name = $row["product_name"];
                    $details= $row["details"];
                $category=$row["category"];
                $subcategory=$row["subcategory"];
            $search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><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>

I tried above code as well and narrow down to product_name, it still not showing eg I have this product_name entered as 'omega fish' if i type 'fish' it doesn't show a single thing, if i type'omega' then it works

I have given this up and tried with the LIKE function and it seems to be better but still funny..please refer to this link for those that's interested

PHP search script for mySQL database, only 3 letter working

Thanks

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-08-12 13:10
    关注

    Please refer to this PHP search script for mySQL database, only 3 letter working i think this one is similar problem, just change the $count >1 to $count =0 or $count >=1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示