Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL utf 8 encoding

Tags:

php

mysql

utf-8

My trying to make an Ajax call to a PHP function that pulls out data from my database. I've run into a problem though.

My query looks like this

$query = "SELECT * FROM mytable WHERE field LIKE '%$string%'"

I then check on the number of rows returned by the query, but when i type in æ ø å then i my query returns 0 rows, although I know there are entries in the database that have æ ø å.. why is this

like image 564
Oldenborg Avatar asked Sep 30 '11 12:09

Oldenborg


1 Answers

Set the connection to use UTF-8:

<?php

// MySQLi:

$connection = new MySQLi( /* ... credentials ...*/);
$connection->set_charset("utf8");


// MySQL:
$connection = mysql_connect(/* ... credentials ... */); 
mysql_set_charset("utf8", $connection);

?>
like image 51
daiscog Avatar answered Oct 21 '22 21:10

daiscog