1

我有一个带有 FirstName 和 LastName 列的 mySQL 数据库,目前我有一个搜索 Lastnames 的搜索查询,当 LastNames 匹配时,它们会在屏幕上与它所在行的 FirstName 一起回显,现在太好了,我想找到什么out 是如何使查询搜索输入到输入字段中的 FullNames,请记住,我没有 FullName 列,全名将是用户输入的内容,下面是我目前拥有的示例在职的!谢谢。

if(isset($_GET['name'])){   
    $raw_name=$_GET['name'];

    if(preg_match("/[A-Z | a-z]+/", $raw_name)){        
        $name=$raw_name;        
        include "connect.php";      
        //-query the database table
        $sql="SELECT userId, FirstName, LastName FROM residential2 WHERE FirstName='" . $name   ."'";
        //-run the query against the mysql query function
        $result=mysql_query($sql);
        //-count results
        $numrows=mysql_num_rows($result);

        echo "<div align=\"center\">" . $numrows . " results found for " . stripslashes($name) . "</div>";

        while($row=mysql_fetch_array($result)){         
            $FirstName =$row['FirstName'];
            $LastName=$row['LastName'];
            $userId=$row['userId']; 
            //-display the result of the array
            echo "<ul>\n"; 
            echo "<li>" . "<a href=\"index.php?id=$userId\">" . $LastName . " "  .$FirstName . "   </a></li>\n";
            echo "</ul>";
        }
    }
    else {      
        echo "<p>Please enter a search query</p>";
    }
}
4

6 回答 6

2

它非常非常简单!

SELECT * FROM your_table WHERE CONCAT( field1, ' ' , filed2 ) like '% $query %'

于 2013-11-08T07:30:09.370 回答
2

利用concat()

$sql="SELECT userId, CONCAT(FirstName, ' ', LastName) AS fullname FROM residential2 WHERE FirstName='" . $name   ."'";

<?php
    $fullname = $row['fullname']; 
?> 
于 2012-06-12T18:46:24.273 回答
1

你可以这样做:

$sql="SELECT userId, FirstName, LastName 
            FROM residential2 
            WHERE concat(FirstName, ' ', LastName) like '%$name%'";
于 2012-06-12T18:47:56.457 回答
0

您必须将全名拆分为名字和姓氏,然后进行相应搜索。给你的代码片段:

SELECT REVERSE(SUBSTR(REVERSE(name),INSTR(REVERSE(name),' ')+1)) AS first_name,
REVERSE(SUBSTR(REVERSE(name),1,INSTR(REVERSE(name),' '))) AS last_name

希望能帮助到你。

于 2012-06-12T18:53:53.897 回答
0
SELECT lname, fname FROM TABLE where CONCAT(fname, ' ', lname) LIKE '%$fullname%'
于 2021-12-25T12:04:11.500 回答
0
$frnd_name = trim(preg_replace( "/[ ]+/", " ", $_GET['frnd_search'] ) );
$arr = explode(" ", $frnd_name, 2);

if(!empty($arr[1]))
    $sql_where = "name LIKE '%".$arr[0]."%' AND lastname LIKE '%".$arr[1]."%'";
else
    $sql_where = "name LIKE '%".$arr[0]."%' OR lastname LIKE '%".$arr[0]."%' OR username LIKE '%".$arr[0]."%' OR email LIKE '%".$arr[0]."%'";

$sql = "SELECT name,lastname,username,email FROM user WHERE ".$sql_where;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "".$row['name']." ".$row['lastname']."<br>";
    }
}
于 2017-03-19T11:43:16.340 回答