I have a mySQL database with a FirstName and LastName column and presently i have a search query searching for Lastnames, and When the LastNames match they are echoed on screen along with the FirstName from it's row, Now thats great, what i will like to find out is how can a make the query search for FullNames that are typed into the input feild, keep in mind that i do not have a FullName column the full name will be what the user types in, bellow is an example of what I presently have working! Thanks.
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>";
}
}
Use concat()
$sql="SELECT userId, CONCAT(FirstName, ' ', LastName) AS fullname FROM residential2 WHERE FirstName='" . $name ."'";
<?php
$fullname = $row['fullname'];
?>
You can do this:
$sql="SELECT userId, FirstName, LastName
FROM residential2
WHERE concat(FirstName, ' ', LastName) like '%$name%'";
its very very simple !!
SELECT * FROM your_table WHERE CONCAT( field1, ' ' , filed2 ) like '% $query %'
You will have to split the full name into first name and last name and then search accordingly. A code snippet for you:
SELECT REVERSE(SUBSTR(REVERSE(name),INSTR(REVERSE(name),' ')+1)) AS first_name,
REVERSE(SUBSTR(REVERSE(name),1,INSTR(REVERSE(name),' '))) AS last_name
Hope it helps.
$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>";
}
}
SELECT lname, fname FROM TABLE where CONCAT(fname, ' ', lname) LIKE '%$fullname%'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.