简体   繁体   English

MySQL UTF-8字符插入问题

[英]MySQL UTF-8 Character insert issue

I am fetching some string from facebook , but i dont know whitch encoding in string . 我正在从Facebook获取一些字符串,但是我不知道字符串中的鞭子编码。 I need to convert this string into utf8 before inserting into database table . 在插入数据库表之前,我需要将此字符串转换为utf8。 Getting this error message. 获取此错误消息。 错误截图

Here is my php code. 这是我的PHP代码。

$email = (isset($this->_userinfo['email']) ? $this->_userinfo['email'] : '');
$fname = $this->_userinfo['first_name'];
$lname = $this->_userinfo['last_name'];
$name  = $this->_userinfo['name'];

$sql = 'INSERT INTO users '
     . '(fbid, fbuid, fullname, userlevel, email, name, sirname) '
     . 'VALUES("'
     . $this->_fbid . '","'
     . $fbuid . '","'
     . $name . '","' 
     . $userlevel . '","' 
     . $email . '","' 
     . $fname . '","' 
     . $lname . '")';

您是否尝试过此代码?

   mysql_query('set names utf8'); 

Take a look at utf8_encode to do this for you. 看看utf8_encode为您完成此操作。 Keep in mind this will only work if your data is actually UTF-8 encoded. 请记住,这仅在您的数据实际上是UTF-8编码的情况下才有效。 Unfortunately there is no way to just look at the string and see what encoding it's using. 不幸的是,没有办法仅查看字符串并查看其使用的编码。

You might want to take a look at this question: 您可能想看一下这个问题:

Detect encoding and make everything UTF-8 检测编码并制作一切UTF-8

especially the second answer by Sebastian Grinoli 特别是塞巴斯蒂安·格里诺利Sebastian Grinoli)的第二个答案

He wrote a class (and offers the link to it) which would correctly encode Windows Extended ASCII to UTF8 and also correct UTF8 if necessary. 他编写了一个类(并提供了指向它的链接),该类将Windows Extended ASCII正确编码为UTF8,并在必要时更正UTF8。

A really handy tool to have when you are in the UTF8 land :) 当您在UTF8领域时,这是一个非常方便的工具:)

enter link description here 在此处输入链接说明

For me works following code: 对我来说,下面的代码:

$mysqli = mysqli_connect( ... ); $ mysqli = mysqli_connect(...); mysqli_query( $mysqli, 'SET NAMES "utf8" COLLATE "utf8_general_ci"' ); mysqli_query($ mysqli,'SET NAMES“ utf8” COLLATE“ utf8_general_ci”');

or just: 要不就:

mysqli_set_charset( $mysqli, 'utf8' ); mysqli_set_charset($ mysqli,'utf8');

Regards, good luck! 祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM