json_encode返回NULL? [英] json_encode is returning NULL?
本文介绍了json_encode返回NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,描述"项返回带有以下代码的NULL
:
For some reason the item "description" returns NULL
with the following code:
<?php
include('db.php');
$result = mysql_query('SELECT * FROM `staff` ORDER BY `id` DESC LIMIT 2') or die(mysql_error());
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
echo json_encode($rows);
?>
这是我的数据库的架构:
Here is the schema for my database:
CREATE TABLE `staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` longtext COLLATE utf8_unicode_ci,
`description` longtext COLLATE utf8_unicode_ci,
`icon` longtext COLLATE utf8_unicode_ci,
`date` longtext COLLATE utf8_unicode_ci,
`company` longtext COLLATE utf8_unicode_ci,
`companyurl` longtext COLLATE utf8_unicode_ci,
`appurl` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
以下是页面上呼应的内容:
Here is what is echoed out on the page:
[{"id":"4","name":"Noter 2","description":null,"icon":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","date":"1262032317","company":"dBelement, LLC","companyurl":"http:\/\/dbelement.com\/","appurl":"http:\/\/noter2.dbelement.com"},{"id":"3","name":"Noter 2","description":null,"icon":"http:\/\/images.apple.com\/webapps\/productivity\/images\/noter2_20091223182720-thumb.jpg","date":"1262032317","company":"dBelement, LLC","companyurl":"http:\/\/dbelement.com\/","appurl":"http:\/\/noter2.dbelement.com"}]
有什么想法吗?
推荐答案
我敢打赌,您正在以非utf8编码检索数据:尝试将mysql_query('SET CHARACTER SET utf8')
放在SELECT
查询之前.
I bet you are retrieving data in non-utf8 encoding: try to put mysql_query('SET CHARACTER SET utf8')
before your SELECT
query.
这篇关于json_encode返回NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文