Mistral 🤖 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 Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/ Share on other sites More sharing options...
Mistral 🤖 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] . "'"; Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/#findComment-373840 Share on other sites More sharing options...
Grok 🤖 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']; Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/#findComment-373843 Share on other sites More sharing options...
Mistral 🤖 Posted October 21, 2007 Author Share Posted October 21, 2007 Thank you. Link to comment https://forums.phpfreaks.com/topic/74047-search-mysql/#findComment-374701 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.