php While循环导致重复结果

php While循环导致重复结果,php,json,while-loop,Php,Json,While Loop,出于某种原因,我的php while循环将数据打印了两次,我似乎无法理解这是为什么。这是我的密码: <?php $json = '['; $query = "SELECT user_id, first_name, last_name, phone, phone_two, email FROM customers LIMIT 20"; $result = mysqli_query($dbc, $query); while($row = mysqli_fetch_array($result,

出于某种原因,我的php while循环将数据打印了两次,我似乎无法理解这是为什么。这是我的密码:

<?php
$json = '[';
$query  = "SELECT user_id, first_name, last_name, phone, phone_two, email FROM customers LIMIT 20";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    $json .= "{ num: \"{$row['user_id']}\" },{ num: \"{$row['first_name']}\" },{ num: \"{$row['last_name']}\" },{ num: \"{$row['phone']}\" },{ num: \"{$row['phone_two']}\" },{ num: \"{$row['email']}\" },";
}

//Remove the last trailing comma
$json .= substr($json, 0, -1);

$json .= ']';

echo $json;
?>


当我将结果反馈到web浏览器时,我的客户不是20个,而是40个。然而,它是前20位两次上市的客户。我不知道是什么原因导致了这种情况。

删除
=
=

那么,你的声明

$json.=substr($json,0,-1)

变成


$json=substr($json,0,-1)

你为什么要这么做?只需在循环中构建一个PHP数组,然后使用
json\u encode
。我同意使用标准的json\u encode方法。同样,最好使用rtrim($json,,'),以防没有尾随(比如当没有用户返回时)