gpt4 book ai didi

用于 mySQL 数据库的 PHP 搜索脚本,只能使用 3 个字母

转载 作者:行者123 更新时间:2023-11-29 04:46:32 25 4
gpt4 key购买 nike

我正在尝试对 mySQL 数据库进行 php 搜索。下面的代码很有趣,当我只输入 3 个字母时它检测得很好..例如我有一个产品名称“deepbluehealth omega”,如果我键入“ome”,它会被拾取,如果我键入“ega”,它会被拾取,如果我输入“omega”没有显示结果,如果我输入“deepbluehealth”,它也没有问题。

<?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 id, product_name FROM products WHERE product_name LIKE '%$searchquery%' OR details LIKE '%$searchquery%') ";
}
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>
</html>

最佳答案

这是你的问题:

if($count > 1){

这需要:

if($count > 0){

考虑只有一个结果的情况。可能这是唯一匹配“omega”的产品,但在所有其他情况下,另一种产品恰好匹配。

关于用于 mySQL 数据库的 PHP 搜索脚本,只能使用 3 个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18187221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com