我让这个人搜索功能建立了 jquery(json) 和 php,从搜索栏中发送到 php 文档,但它的行为不像我想要的那样。
我不知道 mysql 服务器或 php 代码是否有问题,但是当我执行搜索脚本时,它不会返回(示例)上的值:“e”、“s”或“r”,但如果我输入一个人的全名,或者只是一个“a”,它返回所有带有“a”的名字。
这是我的脚本的一个小片段:
$string = trim(mysql_real_escape_string($_POST['string']));
$sql = "SELECT * FROM users WHERE firstName LIKE '%$string%' OR lastName LIKE '%$string%' ORDER BY firstName ASC";
foreach($db->query($sql) as $row) {
//usage of $row like: $name = $row['firstName']." ".$row['lastName'];
}
更新:
继承人整个脚本:
$sql = "SELECT * FROM users WHERE firstName like '%$string%' OR lastName like '%$string%' ORDER BY firstName ASC";
foreach($db->query($sql) as $row) {
$name = $row['firstName']. " ".$row['lastName'];//combing firstname with lastname
//replacing æ,ø,å,Æ,Ø,Å with html friendly charcodes
$remove = array("æ", "ø", "å", "Æ", "Ø", "Å");
$with = array("æ", "ø", "å", "Æ", "Ø", "Å");
$fname = str_replace($remove, $with, $name);
//composing a answer to the user.
$stringUser[] = "<li class='searchResultHit'><div class='searchResultLeft'><img src='".$row['picture']."'></div><div class='searchResultCenter'><span class='searchResultContent'><a href='user.php?id=".$row['id']."'>".$fname."</a></span></div></li>";
}