如题:目前确定是将获取到的字符串转为unicode编码存储到表中,表的编码是utf-8;现在遇到的问题是:如果使用json_encode()函数多次转义后存储到数据库中的字符串多了好几层的双引号,如果只转义一次存储到数据库中虽然没有了引号,但是 符号不见了,如下图,后面附关键代码,新手咨询存储的正确方式;
关键代码:
function weixininfo($code){
$userinfo = getJson($get_user_info_url);
$wxif = new wxinfo($userinfo["openid"],$userinfo["nickname"],$userinfo["sex"],$userinfo["headimgurl"]);
class Emp {};
$obj = new Emp();
$obj->result = 0;
$obj->info = $wxif;
return $obj;
}
$code = $_GET['code'];
$data = weixininfo($code);
$info = $data->info;
$openid = $info->openid;
$nickname = $info->nickname;
$name = json_encode($nickname);
$sex = $info->sex;
$headurl = $info->headurl;
$sqls = "INSERT INTO mytest(openid,nickname,sex,headurl) VALUES ('$openid',$name,'$sex','$headurl')";
if($conn->query($sqls) === true){
session_start();
$_SESSION["openid"]=$openid;
$conn->close();
// $uri ="./index.php";
// header( "Location: $uri" );
}else{
echo "失败:".$sqls."<br>".$conn->error;
};
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
class wxinfo {
public $openid;
public $nickname;
public $sex;
public $headurl;
public function __construct($openid,$nickname,$sex,$headurl){
$this->openid = $openid;
$this->nickname = $nickname;
$this->sex = $sex;
$this->headurl = $headurl;
}
}
mysql field set to
utf8mb4
-utf8mb4-general_ci
php
If the table supports utf8mb4, change it to utf8mb4. If it is not useful, use utf32. utf8 only supports a small number of emoji expressions
Before querying, execute $conn->query("set names utf8") to ensure that the data transmission process is not garbled
emoji are also characters, there is no need to convert them to json
utf-8 uses 3 bytes for storage, while emoji characters have 4 bytes, so converting utf-8 to utf8mb4 will solve it
Also note: the mysql version must be v5.5.3 or higher