我似乎无法弄清楚问题所在,但我的 PHP 数组没有输出为正确的 JSON。我删除了 json_encode 函数,只是发现我的数组是级联的并且没有正确格式化自己。这是一个输出示例(删除了 json_encode):
Array (
[0] => Array (
[username] => g4m3b0y
[profile_photo] => default.png
[email] => michael@me.com
[first_name] => Michael
[last_name] => Simpson
)
)
Array (
[0] => Array (
[username] => g4m3b0y
[profile_photo] => default.png
[email] => michael@me.com
[first_name] => Michael
[last_name] => Simpson
)
[1] => Array (
[username] => michelle
[profile_photo] => default.png
[email] => michelle@gmail.com
[first_name] => Michelle
[last_name] => Houston
)
)
如您所见,它在单独的数组中创建了重复的结果,因此我对 JSON 输出有疑问。这是我生成结果的代码:
$sql2 = "SELECT DISTINCT username, profile_photo, first_name, last_name, email FROM users WHERE (
first_name LIKE '%$q%'
OR last_name LIKE '%$q%'
) ORDER BY last_name";
$sq2 = mysql_query($sql2);
$st2 = mysql_num_rows($sq2);
if($st2>=1) {
while($a = mysql_fetch_array($sq2)) {
$userRow[] = array(
'username' => $a['username'],
'profile_photo' => $a['profile_photo'],
'email' => $a['email'],
'first_name' => $a['first_name'],
'last_name' => $a['last_name']
);
header('Content-Type: application/json');
echo json_encode($userRow);
}
}
else {
exit;
}