1

I have a PHP file which inserts $mail and $password in my database. See code below. My php file, my BD and my table fields are in utf-8. So what I do not understand is why the mail I send "Philémon" goes as "Philémon" in my table. By the way I know it is not a mail format. It is just for the sake of testing.

My PHP:

<?php
header('Content-Type: text/html; charset=utf-8');
require("php/connect.inc.php");

$mail = mysql_real_escape_string("Philémon");
$password = sha1("pass123");

mysql_query("INSERT INTO ENS_MEMBRES (ENS_MAIL, ENS_PASS) VALUES ('$mail','$password')");
?>
2
  • What's the encoding used with mail()? Commented Jan 28, 2012 at 18:24
  • Your database connection might not be using UTF-8? Not the table itself, but the connection to the DB. Commented Jan 28, 2012 at 18:26

2 Answers 2

5

Your PHP source code is probably UTF-8 encoded; your database connection is probably ISO-8859-1 encoded, it's the default connection encoding of mySQL. That will cause the UTF-8 é to be interpreted as two separate bytes, and stored that way in your mySQL table.

Solution: do a

 mysql_set_charset('utf8'); 

before sending the data; if that function isn't available, use

mysql_query("SET NAMES utf8;");
Sign up to request clarification or add additional context in comments.

3 Comments

Hello Pekka.mysql_set_charset('utf8') solved the problem. You rock! I put header('Content-Type: text/html; charset=utf-8'); Why is it not sufficient?
@user no. That header just tells the browser how to interpret the output that is about to come; it has no effect on anything else
So header(blalbla) has just an effect on what is displayed on the page? Correct?
0

For users currently experiencing this issue, mysql_query has been deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0 instead mysqli_query(procedural) or PDO can be used(refer php documentation). To fix this issue for users implementing procedural extension, try this

$conn = mysqli_connect('host', 'user', 'password', 'dbname');
mysqli_set_charset($conn,"utf8");

This worked perfectly for me!

For users implementing PDO extension, try the following

$pdo = new PDO(
    ''mysql:host=host;dbname=urdbname'',
    "username",
    "password",
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 

Hope this helps someone out there ;)

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.