-1

I tried to put some thai sings into a utf8 (utf8_general_ci) mysql database. The data is from the facebook api. In the JSON it looks like \u0e41\u0e15\u0e07\u0e08\u0e49 (original: แตแจ้) and in the database i got some ???? (question marks).

What is the best solution to save this characters into a database?

This is my database connection with mysqli:

$DB = new mysqli('localhost', 'XXX', 'XXXXXX', 'XXXX');
$DB->query("SET CHARACTER SET 'UTF8'");
$DB->set_charset("UTF8");
Dharman
  • 30,962
  • 25
  • 85
  • 135
poldixd
  • 1,103
  • 2
  • 13
  • 27
  • 1
    If phpMyAdmin shows `???` it does not mean the data is saved in the wrong format in your DB. Try to output the saved data and check then. – powtac Jun 14 '11 at 12:01
  • ppowtac makes an important point. It could just mean that where you're outputting it (phpmyadmin, console) you don't have the correct character encoding set. – Evert Jun 14 '11 at 12:22
  • i use heidiSQL for the output. I created a test script for the output. there are still *???* in the output. The content-type is utf8 and the connection to the database also. – poldixd Jun 14 '11 at 15:04

2 Answers2

2

ok, i found the sulotion....

the table was set to utf8_general_ci but not the utf8_general_ci table column... it was latin1....

Dharman
  • 30,962
  • 25
  • 85
  • 135
poldixd
  • 1,103
  • 2
  • 13
  • 27
  • Also, you must use ```;charset=utf8``` on db connection. For example : ```$db = new PDO("mysql:host=xxx.xxx.xxx.xxx;dbname=db_example;charset=utf8", "db_user", "password"); ``` – Burak.H Jun 30 '22 at 11:10
1

in your db table, set your column collation to utf8_unicode_ci, then :

mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
Ben
  • 305
  • 2
  • 6
  • it doesn't work. I returned the charterset with the function $mysqli->character_set_name() it is "latin1". – poldixd Jun 14 '11 at 15:01
  • There is a method for mysqli: http://php.net/manual/de/mysqli.set-charset.php. I used theme, but i have still ??? in the databse – poldixd Jun 14 '11 at 15:07
  • 1
    try in your case $DB->query("SET NAMES 'utf8'"); – Ben Jun 14 '11 at 16:18
  • Thank you for your comment but it also doesn't work... I looked into the cantao script. They also use mysqli. thats the exactly connection start: `$GLOBALS['TL_CONFIG']['dbCharset'] = 'UTF8';` `@$this->resConnection->set_charset($GLOBALS['TL_CONFIG']['dbCharset']);` Is this a problem from mysqli? – poldixd Jun 14 '11 at 19:44
  • Ok, everything is utf8: `Array ( [Variable_name] => character_set_client [Value] => utf8 ) Array ( [Variable_name] => character_set_connection [Value] => utf8 ) Array ( [Variable_name] => character_set_database [Value] => utf8 ) Array ( [Variable_name] => character_set_filesystem [Value] => binary ) Array ( [Variable_name] => character_set_results [Value] => utf8 ) Array ( [Variable_name] => character_set_server [Value] => utf8 ) Array ( [Variable_name] => character_set_system [Value] => utf8 )` but it doesn't work. :( – poldixd Jun 14 '11 at 20:01
  • This helped me a lot. Thanks Ben. I have set this 3 lines before insert Query and it worked. – Milan Malani Nov 25 '15 at 06:33
  • worth noting: mysql_ was deprecated in php5 and removed in php7 – treyBake Sep 17 '19 at 14:44