gpt4 book ai didi

php - 我怎样才能改进我的搜索功能? PHP + MySQL

转载 作者:行者123 更新时间:2023-11-30 21:50:28 25 4
gpt4 key购买 nike

我是新来的,我开始使用 PHP 和 MySQL 进行编码。
我想在表格中搜索多个“关键字”,例如“Intel i7 7700k”,但我不知道如何实现。

我的 MySQL 表结构如下所示:
编号 |品牌 |家庭 |型号 |核心 |线程
1 |英特尔 | i7 | 7700k | 4 | 8

以下 PHP 搜索有效,但仅针对一个词。

if(isset($_GET['search'])){
$search = $conn->escape_string($_GET['search']);

$query = $conn->query("
SELECT brand, family, model
FROM cpus
WHERE brand LIKE '%{$search}%'
OR family LIKE '%{$search}%'
OR model LIKE '%{$search}%'
");

我试图循环单词数组,但它不起作用。

if(isset($_GET['search'])){
$search = $conn->escape_string($_GET['search']);
$words = explode(' ',$search);
$searchout = foreach ($words as $word){
"SELECT brand, family, model FROM cpus WHERE brand LIKE '%$word%' OR family LIKE '%$word%' OR model LIKE '%$word%'"
}
$query = $conn->query($search_out);

echo "<div class='result-count'>Found " . $query->num_rows . " results.</div>";

if($query->num_rows){
while($r = $query->fetch_object()){

echo "<div class='result'><a href='#'>" . $r->brand . $r->model . "</a></div>";
}
}
}

如何使搜索对多个词起作用?
比如 "Intel i7 7700k""7700k intel i7""i7 7700k intel"

这是我的另一个尝试,但也失败了:

if(isset($_GET['search'])){
$search = $conn->escape_string($_GET['search']);
$words = explode(' ',$search);
$words_count = count($words);
$cpus_columns = ['brand','family','model'];
print_r($cpus_columns);
$mysql_where = "SELECT brand, family, model FROM cpus WHERE ".$cpus_columns[0]." LIKE ".$words[0];
$mysql_loop = for ($i = 1; $i <= $words_count; $i++){
" OR ".$cpus_columns[$i]." LIKE ".$words[$i]
}
$mysql_query = $mysql_where . $mysql_loop;
$query = $conn->query($mysql_query);

echo "<div class='result-count'>Found " . $query->num_rows . " results.</div>";

if($query->num_rows){
while($r = $query->fetch_object()){

echo "<div class='result'><a href='#'>" . $r->brand . $r->model . "</a></div>";
}
}
}

我开始慢慢讨厌 PHP 和 MySQL,4 天了,但无法正常工作,这真是令人难过。

最佳答案

这就是我搜索某物的方式在我的例子中只有名字,但你可以添加更多

$arry = array();
$arry = explode(" ",$POST["search"]);

$query="SELECT * FROM tblfbaccban WHERE ";
for($c=0;$c<sizeof($arry);$c++)
{
$query=$query."Name LIKE'%$arry[$c]%'";
if($c<sizeof($arry)-1)
{
$query=$query." OR ";
}
}
$sql=mysqli_query($mysqli,$query)or die(mysql_error());
while ($row = mysqli_fetch_array($sql, MYSQLI_BOTH))
{
echo $row["Name"]."<br>";
}

关于php - 我怎样才能改进我的搜索功能? PHP + MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47511482/

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