Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Encoding Bug?

Heres my problem. I have a mysql table called quotes. In one of the rows, a quote contains the folloqing characters

‘ and ’

Now the row collation is utf8__unicode__ci

When using MySQL Query Browser and PHPMyAdmin to retrive the rows the quotes come out as intended. How ever when i retrive them from the database using PHP and display them on the screen they come out as boxes

� and �

My html page has UTF-8 encoding, and all the UTF-8 encoding options are set in php:

/* Set UTF-8 settings */
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
mb_http_output('UTF-8');

/* Set ICONV ini settings */
ini_set('iconv.input_encoding', 'UTF-8');
ini_set('iconv.output_encoding', 'UTF-8');
ini_set('iconv.internal_encoding', 'UTF-8');

/* Set ICONV function settings */
iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');

It seems to me that it should work. But it doesnt :S can anyone shed some light onto this please.

like image 553
Mike Stanford Avatar asked Dec 08 '25 12:12

Mike Stanford


2 Answers

You may need to set UTF-8 as the chosen charset on your MySQL connection. If you are using the mysqli PHP driver this means something like this:

$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
$mysqli->set_charset("utf8");
like image 126
Joakim Bodin Avatar answered Dec 10 '25 00:12

Joakim Bodin


@Joakim, thanks so much for setting me in the right direction. I couldnt use your exact code because im using a custom database driver and it works using the old mysql commands not the mysqli class. I tried using mysql_set_charset() but my php version didnt support it (still php 5 tho).

this is the solution i used:

mysql_query("SET CHARACTER SET utf8", $this->connection);
mysql_query("SET NAMES utf8", $this->connection);
like image 40
Mike Stanford Avatar answered Dec 10 '25 00:12

Mike Stanford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!