Ingus Graholskis Ingus Graholskis - 2 months ago 10
PHP Question

search do not search for letter "š"

NOTE: it works on phpMyAdmin
also works if i do this in country query

LIKE '%"Šveice"%'


I have search bar in my website .If I try to find word "Šveice" (Šveice in Latvian = "Switzerland" in English) it will not find anything but if I try to search "veice" it will find it.
All other letters are working.

This is what i got :

GET search

if (isset($_GET['query'])){
$query = htmlentities($_GET['query'], ENT_QUOTES, "UTF-8");
}


input

<input type="search" name="query" placeholder="Search..">


Using on database

varchar(100) utf8_latvian_ci


part of query

(address LIKE '%".$query."%' OR country LIKE '%".$query."%')

Answer

It's because of the function misuse.

If you think of it, a function named htmlentities doesn't seem to be having any relation to the SQL stuff. And it doesn't.

Instead of some irrelevant functions you have to use SQL-related operators. Namely prepared statements. So your PHP code should be

if (isset($_GET['query'])){
    $query = "%".$_GET['query']."%";
}

and SQL like

(address LIKE ? OR country LIKE ?)

then prepare your query and execute it usual way