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 Quote Link to comment 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%'"); Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted August 3, 2008 Author Share Posted August 3, 2008 Oh thank you very very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.