gpt4 book ai didi

php - 如何组合 sql 表中的 2 列来搜索全名

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:24 24 4
gpt4 key购买 nike

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>";
}
}

最佳答案

使用concat()

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

<?php
$fullname = $row['fullname'];
?>

关于php - 如何组合 sql 表中的 2 列来搜索全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com