0

I have a php file that inserts data to a mysql db using pdo. When I try to insert the word référencé in a field in one of my table, it is showing in mysql like référencé
I do not get it as I specified a little everywhere the charset as utf-8. See below.

  1. my table is set to innodb utf-8
  2. my field is set to utf8_general_ci
  3. at the top of my php script i put
    a. header('Content-Type: text/html; charset=utf-8');
    b. mysql_set_charset('utf8');
    c. mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

Here below my pdo connexion function:

function connexion($host, $user, $pass, $db){
    $db_host = $host;  
    $db_user = $user;  
    $db_password = $pass;  
    $db_database = $db;               
    return $connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
}

Hope someone can help me understand. Thank you in advance. Cheers. Marc

5
  • That does not look like PDO. Are you using PDO or mysql_*? Commented Apr 25, 2012 at 10:46
  • Try executing "SET NAMES UTF8" query in MySQL as well. Also how do you display the data? It might be insterted properly but the encoding might brake where you display it. Commented Apr 25, 2012 at 10:46
  • Hello decese. I switched from mysql_ to pdo... Commented Apr 25, 2012 at 10:48
  • And are you setting the connection encoding for that PDO connection somewhere as well? Commented Apr 25, 2012 at 10:50
  • @deceze I hust edited my post and added my pdo connecxion function Commented Apr 25, 2012 at 10:51

1 Answer 1

2

Well, apparently you're not setting the connection encoding for your PDO connection. Do it like so:

return new PDO("mysql:host=$db_host;dbname=$db_database",
               $db_user,
               $db_password,
               array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

See http://php.net/manual/en/ref.pdo-mysql.connection.php.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.