PHP 5 中的 Json 编码问题
我正在尝试使用 php5 将 Json 与我的数据库一起使用,但遇到了奇怪的结果。 该数据库有四个字段 -“id”、“Title”、“Thread”、“date”,但 jason 结果如下所示。
[
{
"0": "1",
"id": "1",
"1": "Title 1",
"Title": "Title 1",
"2": "Thread 1",
"Thread": "Thread 1",
"3": "2011-10-19",
"date": "2011-10-19"
},
{
"0": "2",
"id": "2",
"1": "Title 2",
"Title": "Title 2",
"2": "Thread 2",
"Thread": "Thread 2",
"3": "2011-10-03",
"date": "2011-10-03"
}
]
可以看到结果中有重复的信息。他们来自哪里? 我将附上我编写的代码...... Jason & PHP高手请赐教:'(.. 预先感谢您..我会在等待您的帮助时再次尝试解决它....
private static function queryAndFetch($tableName)
{
$query = "SELECT id, Title, Thread, date From $tableName";
$result = mysqli_query(self::$link, $query);
if(!($result))
{
echo "Error";
exit;
}
// $posts = mysqli_fetch_assoc(self::$result); - Working
self::$fetchedResult = array();
while($row = mysqli_fetch_array($result))
{
self::$fetchedResult[] = $row;
}
}
private static function encode()
{
//print_r(self::$fetchedResult);
//if($format == 'json') {
header('Content-type: application/json');
echo json_encode(self::$fetchedResult);
//}
//echo "hi".json_last_error();
}
}
I'm trying to use Json with my database using php5 but suffering from a weird result.
This database has four fields - 'id', 'Title', 'Thread', 'date' but the jason result looks like the following.
[
{
"0": "1",
"id": "1",
"1": "Title 1",
"Title": "Title 1",
"2": "Thread 1",
"Thread": "Thread 1",
"3": "2011-10-19",
"date": "2011-10-19"
},
{
"0": "2",
"id": "2",
"1": "Title 2",
"Title": "Title 2",
"2": "Thread 2",
"Thread": "Thread 2",
"3": "2011-10-03",
"date": "2011-10-03"
}
]
You can see there are duplicated information in the result. Where are they from??
I will attach the code I've written... Jason & PHP masters, please enlighten me :'(..
Thank you in advance.. I will try to solve it again as I wait for your help....
private static function queryAndFetch($tableName)
{
$query = "SELECT id, Title, Thread, date From $tableName";
$result = mysqli_query(self::$link, $query);
if(!($result))
{
echo "Error";
exit;
}
// $posts = mysqli_fetch_assoc(self::$result); - Working
self::$fetchedResult = array();
while($row = mysqli_fetch_array($result))
{
self::$fetchedResult[] = $row;
}
}
private static function encode()
{
//print_r(self::$fetchedResult);
//if($format == 'json') {
header('Content-type: application/json');
echo json_encode(self::$fetchedResult);
//}
//echo "hi".json_last_error();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mysqli_fetch_array
返回具有关联键和枚举键的结果行。如果您只需要关联数组键,请使用mysqli_fetch_assoc
。mysqli_fetch_array
returns the result rows with both associative and enumerated keys. If you only want the associative array keys, then usemysqli_fetch_assoc
.看起来您的 mysqli_fetch_array($result) 返回的是关联数组而不是索引数组。
尝试此更改:
如果这不起作用,请尝试使用 $row['id']、$row['Title']、$row['Thread'] 和 $row['date']。
或者,为了避免写出每个字段,请专门更改为 mysqli_fetch_assoc($result)。
我怀疑这就是你的问题?
谢谢,
我的流
It looks like your mysqli_fetch_array($result) is returning an associative array rather than indexed array.
Try this change:
If that doesn't work, try using $row['id'], $row['Title'], $row['Thread'] and $row['date'].
Alternatively, to avoid having to write out each field, change to mysqli_fetch_assoc($result) specifically.
I suspect this is the issue you've got?
Thanks,
MyStream