问题描述
我正在尝试在 php 和 mysql 中创建一个搜索引擎.搜索引擎应该能够接受多个值并显示所有可能的结果,我检查了这个线程 php/mysql 搜索多个值,但我需要在 LIKE '$search%'
所在的地方使用全局变量.这是我的sql语句的样子,
I'm trying to create a search engine in php and mysql. The search engine should be able to accept multiple value and display all possible result, i checked this thread php/mysql search for multiple values, but i need to use global variable at the place where LIKE '$search%'
is. This is how my sql statement looks like,
SELECT name FROM product WHERE name LIKE '%$search%
变量搜索声明正确,现在当我专门搜索时一切正常,例如Gold chain会显示Gold chain.但是当我一起搜索另一个名称时,例如Gold Chain Shirt,其中衬衫是另一个产品的名称,结果是不显示.我应该如何更改我的 sql 命令以从搜索的多个值中获取多个结果?很抱歉我没有提前告诉我被要求在 3 层编程中做这件事.
the variable search is declared correctly,now everything works fine when i search specifically, such as Gold chain will show Gold chain.But when i search another name together such Gold Chain Shirt,where shirt is another product's name,the result is not showing. How should i change my sql command to get multiple result from multiple value searched? I'm very sorry i did not tell earlier that i was asked to do it in 3 tier programming.
推荐答案
有一篇不错的文章 here 它将为您提供使用 PHP 搜索 MySQL 的体面介绍,但基本上您想要做的是将您的搜索短语分成几部分,然后在 MySQL 查询中使用它们.例如:
There's a decent article here which will give you a decent introduction to searching MySQL with PHP, but basically what you want to do is split your search phrase in to parts and then use them in the MySQL query. For instance:
<?php
$search = 'Gold Chain Shirt';
$bits = explode(' ', $search);
$sql = "SELECT name FROM product WHERE name LIKE '%" . implode("%' OR name LIKE '%", $bits) . "%'";
以上将生成此查询:
SELECT name FROM product WHERE name LIKE '%Gold%' OR name LIKE '%Chain%' OR name LIKE '%Shirt%'
这篇关于php和mysql中的多个搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!