使用名字和姓氏查询搜索用户

时间:2016-11-01 14:57:50

标签: php search

人们可以搜索" Jonathan Steve"

如果我只键入Jonathan,我会得到结果。但如果我输入Jonathan Steve,那么php没有答案

数据库: user_first | user_last 乔纳森史蒂夫

用户应输入一个输入" Jonathan Steve"并且应该得到结果

修改

<?php
session_start();

require_once("../config.php");
require_once("../inc/function.php");    

$query = htmlspecialchars($mysqli->real_escape_string($_GET['q']));

$query = explode(" ", $query);

foreach($query as $search)
{
    $searching = $mysqli->query("SELECT * FROM user_tbl WHERE LOWER(user_first) LIKE LOWER('%$search%') OR LOWER(user_last) LIKE LOWER('%$search%') OR LOWER(user_uname) LIKE LOWER('%$search%')");
    echo "<h5 style=\"padding-left: 5px;\">Personen</h5>";
    while($result = $searching->fetch_assoc())
    {
        $name = $result['user_first']." ".$result['user_last'];
        $uname = $result['user_uname'];
        $image = $result['user_img'];
        $fid = $result['user_id'];

        if(empty($image))
        {
            $image = "nop.jpg";
        }
        else
        {
            $image = "$fid/profileimages/$image";   
        }
        echo "<div class='search-user'><a href='profile.php?p=$uname'><div style='background: url(_usr/$image) no-repeat center center; background-size: cover' class='imgdiv'></div><span>$name</span></a></div>";
    }
}

有什么问题?

2 个答案:

答案 0 :(得分:0)

如果&#34; Jonathan Steve&#34;是名字和姓氏,您的代码正在尝试将其与user_firstuser_last匹配,但不能同时将其匹配。 user_first中没有任何内容匹配&#34; Jonathan Steve&#34;这同样适用于user_last

请改为尝试:

$searching = $mysqli->query("SELECT * FROM user_tbl WHERE 
    LOWER(concat_ws(' ', user_first, user_last, user_uname)) LIKE LOWER('%$search%')");

答案 1 :(得分:-1)

从我可以告诉下面的代码是你想要的。 而不是搜索&#34; Mark&#34;或者&#34; Phillips&#34;,你应该搜索&#34; Mark&#34; &安培; &#34;十字&#34;

如果有两个用户名为&#34; Mark&#34;,即使不是我,也会匹配第一个用户。

试一试,完全未经测试,我希望它有所帮助。

<?php
    session_start();

    require_once("../config.php");
    require_once("../inc/function.php");    

    //First "explode" their name
    $personsFullName = strtolower(htmlspecialchars($_GET['q']));
    $personsNameExplosion = explode(" ", $personsFullName);
    $firstName = null;

    //If the explosion contains multiple elements, sort it into seperate variables for code-reading value
    if ( sizeof($personsnameExplosion) > 1 )
    {
        $firstName = $personsNameExplosion[0];
        $secondName = $personsNameExplosion[1];
    }

    //Start to build the query
    $query = "SELECT * FROM user_tbl WHERE ";

    //If we set $firstName, change the query.
    if ( $firstName != null )
        $query .= "LOWER(user_first) LIKE '%$firstName%' AND LOWER(user_last) LIKE '%$secondName%'"
    else
        $query = "LOWER(user_first) LIKE '%$personsFullName%' OR LOWER(user_last) LIKE '%$personsFullName%' OR LOWER(user_uname) LIKE '%$personsFullName%'"

    //Run the query:
    //Note: Please learn about MySQLi bindings to prevent SQL injection.
    $queryResult = $mysqli->query($query);
    echo "<h5 style=\"padding-left: 5px;\">Personen</h5>";
    while($result = $queryResult->fetch_assoc())
    {
        $name = $result['user_first']." ".$result['user_last'];
        $uname = $result['user_uname'];
        $image = $result['user_img'];
        $fid = $result['user_id'];

        if(empty($image))
        {
            $image = "nop.jpg";
        }
        else
        {
            $image = "$fid/profileimages/$image";   
        }
        echo "<div class='search-user'><a href='profile.php?p=$uname'><div style='background: url(_usr/$image) no-repeat center center; background-size: cover' class='imgdiv'></div><span>$name</span></a></div>";
    }
?>