我正在使这个人搜索功能构建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>";
}