As shown in the title: It is currently confirmed that the obtained string is converted to unicode encoding and stored in the table. The encoding of the table is utf-8; the problem encountered now is: if the json_encode() function is used to escape multiple times and then stored The string in the database has several layers of double quotes. If it is escaped only once and stored in the database, although there are no quotes, the symbols are missing, as shown below, followed by the key code. Newbies can consult on the correct way to store; p>
< /p>
Key code:
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 "Failed:".$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