MySQL: Palabra encriptada al insertar datos con acento o ñ en una tabla

I use a simple Insert to save data (word with accents, ñ) of user registration in a mysql table, but when I use this script in local works without issue but when I use in a host, is saved as If encrypted and I don't understand.

PHP

$query = "INSERT INTO usuarios 
    (user, password, name, lastname, phone, email)
        VALUES ('$user','$pass','$name','$lname','$phone','$email')";

$result = mysql_query($query) or die (mysql_error());

Connection With DataBase

require_once 'config.php';
    //connecting to mysql
    $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
    //selecting database
    mysql_select_db(DB_DATABASE);
    mysql_query('SET names=utf8');  
    return $con;
}

My table and database has utf8 and in my HTML FORM I have

<meta charset="utf-8">

And for example, if I insert these data:

$user = 'soldier';
$pass = '1234';
$name = 'Edgar';
$lname = 'Ramírez';
$phone = '234234';
$email = 'soldier@mail.com';

This is a result that was stored in the table:

Table: Users

user    |    password    |    name    |       lastname       | phone  | email
________________________________________________________________________________________
soldier       1234            Edgar      52616dc383c2ad72657a   234234  soldier@mail.com

And that happens all the time, if a word has ñ or accent, is inserted in the table that way :/

Espero haberme explicado!

preguntado el 25 de agosto de 12 a las 22:08

Más código por favor. lastname is obviously changed somehow somewhere. -

tenga en cuenta que el mysql_xx() functions are considered obsolete. It is fuertemente recommended to upgrade to either the mysqli_xx() alternatives or the PDO library. -

@DanielM Not change in any part of code, only I send data of form using jquery.ajax() then I received in a function PHP and Insert! thats all. -

@Spudley thanks for the recommendation! -

1 Respuestas

You should check the way you create your output. The given text is simply "hex-coded". See the following code:

$text = '52616dc383c2ad72657a';
for ($l = strlen($text), $i = 0; $i < $l; ++$i) {
  echo chr(hexdec(substr($text, $i * 2, 2)));
}

El resultado es Ramírez. The "broken" character is simply the ASCII representation of UTF-8.

So your code is fine, just the tool you use to check the contents of the table shows the string representation in hex rather than as UTF-8.

Respondido 25 ago 12, 22:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.