我的问题是:我的 json_encoded 输出是否正确,如果不正确,如何修复?
我正在尝试查询数据库以获取所有共享一部手机的用户列表。一旦我有了那个列表,我想将它存储在一个 json 数组中,这样它就可以被 xcode 应用程序获取。然后,在 xcode 应用程序上,用户将能够选择他们正在寻找的用户(我不是在构建应用程序,只是为开发人员提供一种与我的数据库对话的方式)。
用户在应用程序中输入一个电话号码,该应用程序被发布到一个页面,我在我的 php 中捕获它,如下所示:
mysql_select_db($database_dbcon, $dbcon);
$query = sprintf("SELECT id, name, surname, perm, branch, mob, branchaddress1, branchpost FROM users WHERE mob=%s AND (status=%s OR status=%s OR status=%s)", GetSQLValueString($_POST['phoneNumber'], "text"), GetSQLValueString('1', "int"), GetSQLValueString('3', "int"), GetSQLValueString('8', "int"));
$Result1 = mysql_query($query, $dbcon);
$userNum = mysql_num_rows($Result1);
if ($userNum > 0) {
$array = array();
while ($userDet = mysql_fetch_assoc($Result1)) {
$array[] = $userDet['forename'] . ',' . $userDet['surname'] . ',' . $userDet['mob'] . ',' . $userDet['branchaddress1'] . ',' . $userDet['branchpost'];
}
echo json_encode($array);
} else {
$false = 'false';
echo json_encode($false);
}
当前正在输出
["john,doe,0777777777,some address,some postcode","jane,doe,0777777777,some address,some post","john,doe,0777777777,some address,some postcode","]
我认为我的结果可能是错误的,因为我看不出开发人员如何能够区分每个帐户,因为它们似乎都相互渗透。
最佳答案
你需要在你的循环中做:
while($userDet = mysql_fetch_assoc($Result1))
{
$array[$userDet['id']] = $userDet['forename'].','.$userDet['surname'].','.$userDet['mob'].','.$userDet['branchaddress1'].','.$userDet['branchpost'];
}
但更好的方法是
while($userDet = mysql_fetch_assoc($Result1))
{
$array[$userDet['id']] = array(
'forename' => $userDet['forename'],
'surname' => $userDet['surname'],
[…]
);
}
关于php - 从 php 数据库查询创建个人用户帐户的 Json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742297/