gpt4 book ai didi

php - 进行搜索查询,其中只有最匹配的单词才应返回 post_ID

转载 作者:行者123 更新时间:2023-11-29 18:22:20 25 4
gpt4 key购买 nike

我创建了一个带有提货输入的表单,因此当用户输入德里机场作为提货位置并输入阿格拉作为投递位置时,它应该返回 post_ID = 2 但我已经使用 WHERE LIKE 进行了查询这导致所有 ID 在 post_title 中都包含单词“Delhi”。

这里有人可以帮助我获得准确的结果吗?

提前致谢。

对于产品 1:

post_ID = 1
column name | Value
post_title | Delhi, Agra //syntax pick location, drop location
post_name | delhi-agra //syntax pick location-drop location

对于产品 2:

post_ID = 2
column name | Value
post_title | Delhi Airport, Agra
post_name | delhi-airport-agra

此处,德里、德里机场和阿格拉是 woocommerce 变体的组合。

查询:

global $wpdb;

$q1 = $wpdb->get_results("select * from table_name where post_type='product_variation' and post_title like '%".$pick[0]."%, %".$drop[0]."%'");

我知道此查询将被更改,但我不确定应该使用哪种方法来获取我想要的 ID。

最佳答案

$search_query = $_POST['the_search_query']; //... Get search query, be sure to validate

//... Connect to Database, then:
$results = array();
$stmt = $dbc->prepare("SELECT item_id, other_stuff FROM tablename WHERE MATCH(item_title) AGAINST(? IN NATURAL LANGUAGE MODE) LIMIT 5");
$stmt->bind_param('s', $search_query);
$stmt->execute();
$stmt->store_result();
if (($n_res = $stmt->num_rows) > 0): // $n_res can tell you how many results are found
$stmt->bind_result($item_id, $other_stuff);
while ($row = $stmt->fetch()):
$results[] = array($item_id, htmlspecialchars($other_stuff));
endwhile;
$stmt->free_result();
else:
echo 'error';
endif;
$stmt->close();
$dbc->close();

显示结果示例

<?php
if ($results):
?><ul><?php
foreach($results as $r):
?><li><?php echo $r[0].' '.$r[1]; ?></li><?php
endforeach;
?></ul><?php
endif;
?>

当使用 MATCH() AGAINST() 时,您可能需要向表中添加 FULLTEXT KEY,例如:

CREATE TABLE something (
...,
...,
FULLTEXT KEY items_i_want_to_match (item_title, other_stuff)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

关于php - 进行搜索查询,其中只有最匹配的单词才应返回 post_ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46469106/

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