0

Im trying to insert 汉语/漢語 characters into my database but im only getting ????? when i do. Iv look at loads of information on line but no solution works. When i run an insert query in my db with the characters 汉语/漢語 it works, so I know my db is set-up for utf8...Its somthing im doing in my PHP file that's the problem...any help would be greatly appreciated.

<?php
include 'config.php';  
header('Content-Type:text/html; charset=UTF-8');
mysqli_query("SET NAMES utf8");
mysqli_set_charset('utf8');


// check for required fields
if (isset($_POST['name'])) {
$name = mysqli_real_escape_string($link, $_POST['name']);

$result = mysqli_query($link, "INSERT INTO scores(`id` , `name`)VALUES(NULL, '$name'");
Dean Clancy
  • 517
  • 3
  • 6
  • 16

2 Answers2

3

You need to make sure your database is operating in UTF-8 mode. You need to do this on the table itself. You said you INSERTED the string, but did you check to see if you can read it back out? As noted in my comment below, you should be sure that you're connecting to MySQL with UTF-8 enabled. There is more information in the answer below:

How to make MySQL handle UTF-8 properly

Additionally, be sure that the PHP file itself is being saved in UTF-8 format. Last but not least ensure the HTML page is correctly set to use utf-8:

<meta charset="utf-8"> vs <meta http-equiv="Content-Type">

Community
  • 1
  • 1
Brian
  • 6,910
  • 8
  • 44
  • 82
  • Hey thanks for the reply. Yes...ive inserted the characters o.k and its reading it o.k. Iv narrowed it down to the php file...The database is o.k – Dean Clancy Aug 24 '13 at 14:44
  • You should verify that mysqli_set_charset('utf8'); is actually returning true. The docs show the signature to be bool mysqli_set_charset ( mysqli $link , string $charset ). – Brian Aug 24 '13 at 14:48
  • I ran into a similar project I a working on. Tables and database were set to UTF-8, but the CONNECTION to the DB wasn't set to UTF-8 so none of the localized text was getting stored correctly. – Brian Aug 24 '13 at 14:49
  • How to check connection to db? – Dean Clancy Aug 24 '13 at 15:00
  • @BrianV he is setting the connection encoding in his code above, though. – Pekka Aug 24 '13 at 15:00
  • Yes, but it hasn't been verified if it's setting the encoding successfully. A quick test would be $result = mysqli_query($link, "INSERT INTO scores(`id` , `name`)VALUES(NULL, '汉语/漢語'"); and then reading it back out, if it comes out ????? then you know it's not being set. – Brian Aug 24 '13 at 15:02
  • Hey @BrianV...iv tried what you said...and im still getting ????? i have log the input in android studio and its showing me the Chinese characters....so the data been sent is correct and when I set the name variable to 汉语/漢語 in the php file it shows ???? when i run the insert...but when i run the insert for phpmyadmin it shows the correct characters... – Dean Clancy Aug 24 '13 at 16:09
  • Dean, one more thing to check - make sure you are SAVING the PHP file in utf-8. See my edits up top. – Brian Aug 26 '13 at 13:31
1

Here's an old cheat sheet I rely on. Keep mind that some of this info is out of date

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

// only for legacy MySQL_query

mysql_set_charset('utf8',$con);

// only for MySQLi

$mysqli->set_charset("utf8")

also check out: http://tympanus.net/codrops/2009/08/31/solving-php-mysql-utf-8-issues/

PHP didn't use to be natively UTF-8 friendly, you had to rely on secondary functions like these below. I'm pretty sure all of the functions on the left have become UTF-8 friendly for a few years now.

mail()                -> mb_send_mail()
strlen()              -> mb_strlen()   
strpos()              -> mb_strpos()
strrpos()             -> mb_strrpos()
substr()              -> mb_substr()
strtolower()          -> mb_strtolower()
strtoupper()          -> mb_strtoupper()
substr_count()        -> mb_substr_count()
htmlentities($var)    -> htmlentities($var, ENT_QUOTES, 'UTF-8')
TravisO
  • 9,406
  • 4
  • 36
  • 44