computer guy Posted October 20, 2007 Share Posted October 20, 2007 Hi I have 2 fields in a mysql database. I need to make it so if a user searches for "john doe" it will search the 'firstname' field for "john" and 'lastname' field for "doe". It must only return 1 result. Except of course if there is more than 1 "john doe". Thank you Quote Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/ Share on other sites More sharing options...
Jeremysr Posted October 20, 2007 Share Posted October 20, 2007 Use explode() to change the name into an array: $name = explode(' ', $searched_name); This splits the variable $searched_name into an array of strings, seperated by a space (or whatever you put as the first argument). Then use this query: $sql = "SELECT * FROM table WHERE firstname = '" . $name[0] . "' AND lastname = '" . $name[1] . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/#findComment-373840 Share on other sites More sharing options...
igor berger Posted October 20, 2007 Share Posted October 20, 2007 use trim() to strip out the end spaces on both sides, so you do not get the wrong result when you use explode. So $name=trim($_POST['name']; Quote Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/#findComment-373843 Share on other sites More sharing options...
computer guy Posted October 21, 2007 Author Share Posted October 21, 2007 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/#findComment-374701 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.