I'm trying to build a simple search function in php that searches values in a SQL database.
My code seems to work ok so far, however I have some issues with special characters. For example I don't get any search results, as far as I can tell, the search is executed as the die command is not called.
$query = mysql_query("SELECT * FROM table WHERE attribute LIKE '%$input%';")
or die("Could not search");
I think the issue is that $input
is a string which can contain things like a&b. How do i deal with this?
Additionally the database contains some French words, so I have to deal with all the é,ç,ô
etc. How do I make sure the outputs from the database that contain special characters, for example hôtel
, are correctly displayed through php?
UPDATE
After spending hours trying to figure out what the issue is, I've found some indications where things might go wrong. Initially I had thought that escaping caused the problems, this does not seem to be the case...
My code creates a string $query_in
which is then passed using
mysql_query($query_in);
I echoed the $query_in
string for debugging:
SELECT * FROM table WHERE attribute1 LIKE
"%a & b%" OR attribute2 LIKE "%a & b%" OR
attribute3 LIKE "%a & b%" OR attribute4
LIKE "%Ta & b%" OR attribute5 LIKE "%a & b%"
OR attribute6 LIKE "%a & b%"
If I manually run (copy&paste) this query in the phpMyadmin interface it works just fine and I get results. However, when I check for empty return values from the database using:
$count = mysql_num_rows($query);
echo $count
returns 0
How can that be? Also, rest assured I'll switch things over to msqli after this is fixed ;)