我创建了一个带有选择输入的表单,所以当用户输入德里机场作为选择位置而agra作为放置位置时,它应该返回post_ID = 2,但我使用WHERE LIKE
进行了查询结果所有ID都在post_title中有“德里”字样。
这里的任何人都可以帮助我获得准确的结果吗?
提前致谢。
对于产品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
这里的德里,德里机场和阿格拉是各种商业变化的组合。
查询:
global $wpdb;
$q1 = $wpdb->get_results("select * from table_name where post_type='product_variation' and post_title like '%".$pick[0]."%, %".$drop[0]."%'");
我知道这个查询会被更改,但我不确定应该使用哪种方法来获取我想要的ID。
答案 0 :(得分:0)
$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;