Canman2005 Posted August 3, 2008 Share Posted August 3, 2008 Hi all I have a simple table, which looks like id first_name surmae -------------------------- 1 david walker 2 sarah turner 3 suzy brick I then have a HTML form which has one input field for "full name". How can I write a select query which allows me to combine the first_name and surname fields of the database and do a full name search hope that make sense, any help would be great thanks dave Link to comment https://forums.phpfreaks.com/topic/117898-solved-query-name/ Share on other sites More sharing options...
JasonLewis Posted August 3, 2008 Share Posted August 3, 2008 Try out CONCAT. $query = mysql_query("SELECT * FROM table WHERE CONCAT(first_name,' ',surname) LIKE '%$full_name%'"); Link to comment https://forums.phpfreaks.com/topic/117898-solved-query-name/#findComment-606468 Share on other sites More sharing options...
genericnumber1 Posted August 3, 2008 Share Posted August 3, 2008 Or, if you are afraid people will post middle names (which you would like to ignore) You could explode() the values, take the first array element and use it as the first name, and the last array element and use it for the last, effectively removing the name from the query. After cleaning the variables you could search for them. For example... $name = explode(' ', $_POST['full_name']); $firstName = strtolower($name[0]); $lastName = strtolower($name[count($name) - 1]); // Clean the first/last name variables here $query = "SELECT * FROM table WHERE first_name = '{$firstName}' AND surname = '{$lastName}'"; // Query stuff here ?> You get the idea. This matches exact first and last names, if you want it to where they can put in parts of names (tim will return timothy for example) you could do a LIKE query such as the person above me posted. Link to comment https://forums.phpfreaks.com/topic/117898-solved-query-name/#findComment-606471 Share on other sites More sharing options...
Canman2005 Posted August 3, 2008 Author Share Posted August 3, 2008 Oh thank you very very much Link to comment https://forums.phpfreaks.com/topic/117898-solved-query-name/#findComment-606472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.