0

So I'm trying to find "Wrocław" in my Mysql table with collation utf8_general_ci.

In PHP, I'm using this code

$query = $mysqli->query("
SELECT * 
  FROM locations
 WHERE location 
  LIKE 'Wrocław'
") or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($query)) { echo'found it'; exit; }

However, I'm not getting any results. I will get a result when searching for "Barcelona". So I'm guessing that the problem is due to the funny character "ł".

When entering the above Query in PhpMyAdmin, I am in fact finding the row containing "Wrocław" which is the expect result.

Any idea why it's not working on the PHP side? I've also tried to previously use mysqli_real_escape_string on Wrocław

Thanks!

user3783243
  • 5,368
  • 5
  • 22
  • 41
James
  • 458
  • 1
  • 6
  • 18
  • What is the encoding of your php source file? – Lars Dec 17 '19 at 12:32
  • When using PHPMyAdmin, and when querying directly, are you using the same version of MySQL? – Strawberry Dec 17 '19 at 12:33
  • @Lars UTF-8 is the encoding – James Dec 17 '19 at 12:35
  • @Strawberry yes – James Dec 17 '19 at 12:36
  • 1
    @EliâMelfior thank you for your comment, "SELECT *" is used here for illustrations purposes only – James Dec 17 '19 at 12:37
  • 1
    Try this: $mysqli->set_charset("utf8mb4"), (it's just a guess) Somehow you have to escape the character More information here: https://www.php.net/manual/pt_BR/mysqli.set-charset.php – Eliâ Melfior Dec 17 '19 at 12:37
  • 4
    Try this: `$mysqli->set_charset('utf8mb4');` or, if you do switch to PDO `$db = new PDO('dblib:host=host;dbname=db;charset=utf8mb4', $user, $pwd);` And see RJ's excellent (if graphically naive) tutorial here: http://mysql.rjweb.org/doc.php/charcoll#php – Strawberry Dec 17 '19 at 12:38
  • If you used prepared statements when both writing and reading, would it help with character encoding issues that this seems to be? It certainly seems to deal with other embedded character issues. – droopsnoot Dec 17 '19 at 12:51
  • Never `or die(mysqli_error($mysqli))` in your code. Enable MySQLi exceptions instead. See https://stackoverflow.com/a/22662582/1839439 – Dharman Dec 17 '19 at 15:34

0 Answers0