php 生成json,输出了NULL,何解?
程序员文章站
2022-06-05 18:10:42
...
$a=mysql_query("SELECT * FROM `qq` WHERE userid='$userid'");
$id= 1;
while($row=mysql_fetch_array($a)){
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user =new User();
$user->id = $id++;
$user->qq = $row["qq"];
$user->txUrl ='http://q.qlogo.cn/headimg_dl?dst_uin='.$row["qq"].'&spec=100&img_type=jpg';
$user->qqName = $name;
$user->pczx = $row["pczx"];
$user->txgj = $row["txgj"];
$user->iphone = $row["iphone"];
$user->qqmusic = $row["qqmusic"];
$user->xunzhang = $row["xunzhang"];
$user->qqgame = $row["qqgame"];
$user->qqpay = $row["qqpay"];
$user->qqvip = $row["qqvip"];
$user->qqhtdy = $row["qqhtdy"];
$user->qqhtyy = $row["qqhtyy"];
$user->time = $row["time"];
$data[]=$user;
}
$json = json_encode($data);
echo "{".'"msg"'.":true,"."\"nr\":".$json."}";
输出后,qqName是null值,这$name明明是有文本值的,却输出null我就想不明白了,特来求问
谢谢,解决了,转码一下
$id= 1;
while($row=mysql_fetch_array($a)){
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user =new User();
$user->id = $id++;
$user->qq = $row["qq"];
$user->txUrl ='http://q.qlogo.cn/headimg_dl?dst_uin='.$row["qq"].'&spec=100&img_type=jpg';
$user->qqName = $name;
$user->pczx = $row["pczx"];
$user->txgj = $row["txgj"];
$user->iphone = $row["iphone"];
$user->qqmusic = $row["qqmusic"];
$user->xunzhang = $row["xunzhang"];
$user->qqgame = $row["qqgame"];
$user->qqpay = $row["qqpay"];
$user->qqvip = $row["qqvip"];
$user->qqhtdy = $row["qqhtdy"];
$user->qqhtyy = $row["qqhtyy"];
$user->time = $row["time"];
$data[]=$user;
}
$json = json_encode($data);
echo "{".'"msg"'.":true,"."\"nr\":".$json."}";
输出后,qqName是null值,这$name明明是有文本值的,却输出null我就想不明白了,特来求问
回复讨论(解决方案)
json_encode 只用于 utf-8 编码
你的 $user->qqName 是这么来的
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user->qqName = $name;
但是
var_dump(mb_check_encoding($name, 'utf-8')); //bool(false)
var_dump(mb_check_encoding($name, 'gbk')); //bool(true)
知道原因了吗?
json_encode 只用于 utf-8 编码
你的 $user->qqName 是这么来的
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user->qqName = $name;
但是
var_dump(mb_check_encoding($name, 'utf-8')); //bool(false)
var_dump(mb_check_encoding($name, 'gbk')); //bool(true)
知道原因了吗?
谢谢,解决了,转码一下