OK, I have a DB table that’s called players and each player has a forename and surname. Then I have a PHP Ajax search thing that I call to search for players. For example… in the input box, someone types James and there’s a row in the table with forename and surname James and Smith respetively.
I do this $check = mysql_query("SELECT * FROMplayersWHEREsurnameLIKE '%$name%' ORforenameLIKE '%$name%' LIMIT 0, 10") or die(mysql_error());
And it returns at least 10 with either forename or surname like the keyword James. However, if I type James Smith, despite it being in the table, I get zero results.
How do I fix this?
Are you using InnoDB or MyISAM? If your using MyISAM, you can create a single field which holds the combined name and then search it using a full text index. So lets imagine you add a new field called combined_names you would search it like this
This would find any row with either John or Smith in, you can change it to match only those rows with both parts you would add plusses like so:
Here is the documentation on the MySQL site where you can find out more:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html