我正在从我的数据库中创建对First_name和Last_name的搜索,从而用户输入他想在字段中找到的内容,并且我的php脚本将搜索类似于名称。如何从Mysql中的两列进行精确搜索
,所以我做
$q=addslashes($_REQUEST['q']);
$q =mysqli_real_escape_string($connection, $q);
//In order to search word by word i explode the string
$notre_tableau=explode(',',$q);
//I count the string in order to search each word
$compter_tableau=count($notre_tableau);
$req_search = "SELECT first_name, last_name
FROM mytable
WHERE ";
//Now i read the array and search for each word
for ($i = 0; $i < $compter_tableau; $i++)
{
$notremotchercher=trim($notre_tableau["$i"]);
if($i==$compter_tableau) { $liaison="AND"; } else { $liaison=""; }
if($i!=0) { $debutliaison="AND"; } else { $debutliaison=""; }
//I search only if the string is not empty
if($notremotchercher!='')
{
$req_search .= "$debutliaison (first_name LIKE '%$notremotchercher%' OR last_name LIKE '%$notremotchercher%') $liaison";
}
}
//Now i proceed with my search
$requete=mysqli_query($connection, "$req_search ORDER BY id DESC LIMIT 20 ") or die(mysqli_error($connection));
因此,如果有人的名字都是珍妮弗·简和姓是DOE,例如,如果在珍(仅珍)我输入它的工作原理, 如果我输入的珍妮弗简它的作品, ,如果我输入美国能源部,它也可以。
上面的代码会显示这样的基于詹妮弗
SELECT first_name, last_name FROM mytable WHERE (first_name LIKE '%Jennifer%' OR last_name LIKE '%Jennifer%')
搜索所以我得到的结果
问题 如果我输入简珍妮弗,它不工作 另外,如果我输入珍妮弗DOE,它不起作用。请知道我无法知道用户在第一个输入内容时如何使它适用于两列?
谢谢
哇。如果名字用双倍空格分隔,怎么样? – 2015-03-02 23:32:24
我不确定它会影响代码 – 2015-03-03 01:01:40