0

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 :/

I hope I have explained!

4
  • More code please. lastname is obviously changed somehow somewhere.
    – Daniel M
    Aug 25, 2012 at 21:35
  • please note that the mysql_xx() functions are considered obsolete. It is strongly recommended to upgrade to either the mysqli_xx() alternatives or the PDO library.
    – Spudley
    Aug 25, 2012 at 21:40
  • @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. Aug 25, 2012 at 21:42
  • @Spudley thanks for the recommendation! Aug 25, 2012 at 21:42

1 Answer 1

0

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)));
}

The result is 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.