1

I have a small script that receive some data through GET and inserts data in a db. My problem is when sending some UTF-8 characters. The scritp receives them ok but inserts them in a weird way. I printed the query in my page, executed it with phpmyadmin and works ok that way. So, my problem is when executing the query through my script (it doesn't work if I execute a constant query with those characters). Does sending characters by post resolve the issue?

Thank you

3
  • You need to specify the encoding for your Mysql connection. Depending on the mysql extension you use, the name of the function can vary: php.net/manual/en/function.mysql-set-charset.php Commented Aug 9, 2011 at 16:39
  • Give more information. "In a weird way" is not useful. Ideally, provide hexdumps of the actual bytes transmitted or stored, not just textual cut-and-pastes from terminal windows. Commented Aug 9, 2011 at 16:40
  • An answer in this possible duplicate How to store special charaters in MySQL - [AîA] formulates it more nicely. Commented Aug 9, 2011 at 16:42

3 Answers 3

1

Try this:

mysql_set_charset('utf8');
Sign up to request clarification or add additional context in comments.

2 Comments

Better use mysql_set_charset() instead of SET NAMES
The main advantage of mysql_set_charset() is that it is safer.
0

Your entire setup has to be UTF-8. That means your web page, PHP, the database connection, AND the database tables, all have to be in UTF-8 mode. Given it works in the admin pages and not via script, I'm guessing it's your database connection. Try doing a set names='utf-8' before doing your insert query, and see if that fixes things. If it does, then your db connection is using some OTHER character set and mangling your text as it goes from PHP->database.

Comments

0

Add this in the php file before your query:

mysql_query('SET NAMES utf8');

For example:

//DB connection
$con = mysql_connect('localhost', 'root', '');//db details
if (!$con){
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);//DB name
mysql_query('SET NAMES utf8');
//query
$sql="SELECT * FROM users WHERE uid=".$user_id;// your query
$result=mysql_query($sql);

1 Comment

Better use mysql_set_charset() instead of SET NAMES

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.