UTF-8 MySQL and Charset

Can someone explain to me when I will set everything up for UTF-8? I keep scooping

MySQL
Server Version: 5.1.44
MySQL charset: UTF-8 Unicode (utf8)

I am creating a new database

name: utf8test
sort: utf8_general_ci
MySQL connection mapping: utf8_general_ci

My SQL is as follows:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; CREATE TABLE IF NOT EXISTS `test_table` ( `test_id` int(11) NOT NULL, `test_text` text NOT NULL, PRIMARY KEY (`test_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `test_table` (`test_id`, `test_text`) VALUES (1, 'hééélo'), (2, 'wööörld'); 

My PHP / HTML:

 <?php $db_conn = mysql_connect("localhost", "root", "") or die("Can't connect to db"); mysql_select_db("utf8test", $db_conn) or die("Can't select db"); // $result = mysql_query("set names 'utf8'"); // this works... why?? $query = "SELECT * FROM test_table"; $result = mysql_query($query); $output = ""; while($row = mysql_fetch_assoc($result)) { $output .= "id: " . $row['test_id'] . " - text: " . $row['test_text'] . "<br />"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="it" xmlns="http://www.w3.org/1999/xhtml" xml:lang="it"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>UTF-8 test</title> </head> <body> <?php echo $output; ?> </body> </html> 
+6
mysql utf-8 character-encoding
source share
6 answers

I installed everything for utf-8

Not really. You must tell mysql your client encoding.

In fact, you do not need to configure "everything" in utf-8. You can have your tables in latin1 and output in utf-8. Or vice versa.
Very flexible.

But you need to configure client encoding explicitly. So why does it work with set names utf8 . Because this request sets up client encoding. And let Mysql know that the data should be sent to utf-8. Pretty reasonable, huh?

Also I have to mention your SQL dump. It needs the same settings. Just FIND A TITLE somewhere upstairs. Because you are sending these requests from some client. And this client encoding must also be configured.

And one more thing: mention that your server sends the correct encoding to the Content-type header. You did not install it in UTF-8 either.

+1
source share

Try setting the charachter encoding after the mysql_connect function as follows:

  mysql_query ("set character_set_client='utf8'"); mysql_query ("set character_set_results='utf8'"); mysql_query ("set collation_connection='utf8_general_ci'"); 
+6
source share

I did not see the query "SET NAMES 'utf8';" immediately after connecting to your database.

Try it, may work for you.

+1
source share

I would say that you forgot to set the encoding of the content type of your PHP file to utf-8:

 header('Content-Type: text/html; charset=utf-8'); 

Or a coding error in a MySQL database?

If only loading the data returns incorrect results, you can use the previously requested or this line of code to enable UTF-8 for queries:

 $mysqli->set_charset('utf8'); 

Hope this is what you need.

+1
source share

take a look at the mysql_set_charset function. You may need to call him before you can recover the data.

0
source share

You want to check the current encoding with mysql_client_encoding and, if necessary, mysql_set_charset . Or just ignore the check and go blindly with the setup.

0
source share

All Articles