1

French words are saving in to database like-

   Sécurité Informatique

The original word is - Sécurité Informatique

How to search this word using LIKE '%Sécurité Informatique%'

I have already tried mysqli_real_escape_string and

WHERE table.content COLLATE UTF8_GENERAL_CI LIKE '%Sécurité Informatique%'
  • 8
    The problem isn't in your search query, it's in the insert. You should really start using UTF8. HTML entities should not have found their way into your database. – Denys Séguret Jul 30 '19 at 07:26
  • If you don't fix your inserts and the data, you will have to search for `'%Sécurité Informatique%'` – Paul Spiegel Jul 30 '19 at 07:35
  • 1
    Go read up on how to _properly_ handle UTF-8 throughout your application here, https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – misorude Jul 30 '19 at 08:14

2 Answers2

1

First you need to insert your data correctly to your database then you can search for it. For doing that you should use correct encoding, which is UTF8 for your usage.

How ? It depends on how you are connected to your database Some examples :

For MySQLi:

mysqli_set_charset($conn, "utf8mb4");

For MySQLi OOP :

$conn->set_charset("utf8mb4");

Also you need to specify you are using UTF-8 In your client side(HTML) with :

<meta http-equiv="Content-type" content="text/html;charset=utf-8" />

The reason why we used utf8mb4 instead of utf8 is utf8 in MySQL is outdated and use 3 bytes but utf8mb4 is the correct utf8 and uses 4 bytes.

M4HdYaR
  • 1,124
  • 11
  • 27
  • Why suggest the outdated utf8 only to make a small footnote at the end about the correct charset? – Dharman Jul 30 '19 at 09:04
  • @Dharman I didn't want to explain why we need to use utf8mbr and confuse the question creator however I've edited my answer. – M4HdYaR Jul 30 '19 at 09:20
-2

Thank you all for your suggestions. The solution I used is similar to,

$temp = "Sécurité Informatique";
$temp = htmlentities($temp);
$temp = htmlspecialchars($temp);
WHERE table.content COLLATE UTF8_GENERAL_CI LIKE '%".$temp."%'