PHP5中的Json编码问题
我试图使用php5在数据库中使用Json,但结果很奇怪。 这个数据库有四个字段-‘id’、‘Title’、‘Thread’、‘date’,但结果如下所示PHP5中的Json编码问题,php,json,fetch,Php,Json,Fetch,我试图使用php5在数据库中使用Json,但结果很奇怪。 这个数据库有四个字段-‘id’、‘Title’、‘Thread’、‘date’,但结果如下所示 [ { "0": "1", "id": "1", "1": "Title 1", "Title": "Title 1", "2": "Thread 1", "Thread": "Thread 1", "3": "2011-10-
[
{
"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"
}
]
您可以看到结果中有重复的信息。他们从哪里来??
我会附上我写的代码。。。贾森和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();
}
}
返回包含关联键和枚举键的结果行。如果您只需要关联数组键,则使用。返回包含关联键和枚举键的结果行。如果您只需要关联数组键,则使用。它看起来像您的mysqli_fetch_数组($result)正在返回关联数组而不是索引数组 尝试以下更改:
while($row = mysqli_fetch_array($result)) {
self::$fetchedResult[] = array(
'id' => $row->id,
'Title' => $row->Title,
'Thread' => $row->Thread,
'date' => $row->date
);
}
如果不起作用,请尝试使用$row['id']、$row['Title']、$row['Thread']和$row['date']
或者,为了避免必须写出每个字段,专门更改为mysqli_fetch_assoc($result)
我怀疑这就是你的问题
谢谢,
MyStream看起来您的mysqli_fetch_数组($result)返回的是关联数组,而不是索引数组 尝试以下更改:
while($row = mysqli_fetch_array($result)) {
self::$fetchedResult[] = array(
'id' => $row->id,
'Title' => $row->Title,
'Thread' => $row->Thread,
'date' => $row->date
);
}
如果不起作用,请尝试使用$row['id']、$row['Title']、$row['Thread']和$row['date']
或者,为了避免必须写出每个字段,专门更改为mysqli_fetch_assoc($result)
我怀疑这就是你的问题
谢谢,
MyStreamWow…我真是太蠢了…我甚至在上面有一个正确的代码并注释掉了…这都是因为我对assoc和数组的误解。感谢您的快速回答!!!!您还可以指定要返回的内容
mysqli\u fetch\u array()
。您可以使用mysqli\u fetch\u array(mysqli\u assoc)
或mysql\u-fetch\u-array(MYSQLI\u-NUM)
tooWow…我真的很蠢…我甚至有一个正确的代码并注释掉了…这都是因为我对assoc和array的误解。谢谢你的快速回答!!!!你也可以指定你想要的MYSQLI\u-fetch\u-array()
返回。您也可以使用mysqli\u fetch\u数组(mysqli\u ASSOC)
或mysql\u fetch\u数组(mysqli\u NUM)