Issue when inserting UTF8 characters in db from script
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 wi开发者_Python百科th 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
Try this:
mysql_set_charset('utf8');
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.
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);
精彩评论