while循环将所有行连接到一个?

时间:2016-07-20 13:53:02

标签: php mysql json

我正在尝试使用外键从表中获取所有行。我的while循环只返回一行,该行包含应该返回的两行中的分散数据。

$ins = 'SELECT * FROM car_reg INNER JOIN User_Details ON car_reg.User_ID = User_Details.User_ID WHERE car_regColumn = userinput AND car_regColumn = userinput';
$qur = $con->query($ins);
if(mysqli_num_rows($qur) > 0){
while{$g = mysqli_fetch_array($qur)
$response = array("status" => 1 "Surname" => $g['fname'], $"Surname" => $g["fname"], "LastName" => $g["lname"],  "CarType" => $g["Car_Type"], "NumberPlate" => $g["Number_plate"], "CarImage" => $g["car_image"], "Gender" => $g["Gender"], "Mobile" => $g["Telephone"], "Price" => $g["Price"], "PickupTime" => $g["arrival_time"]);}}

json_encode($response);

2 个答案:

答案 0 :(得分:1)

您的$reponse已在while循环中被覆盖。声明一个数组并将获取的数据推入其中。试试这段代码:

$response = array();
$ins = 'SELECT * FROM car_reg INNER JOIN User_Details ON car_reg.User_ID = User_Details.User_ID WHERE car_regColumn = userinput AND car_regColumn = userinput';
$qur = $con->query($ins);
if(mysqli_num_rows($qur) > 0){
while($g = mysqli_fetch_array($qur))
{
$response[] = array("status" => 1, "Surname" => $g['fname'], "Surname" => $g["fname"], "LastName" => $g["lname"],  "CarType" => $g["Car_Type"], "NumberPlate" => $g["Number_plate"], "CarImage" => $g["car_image"], "Gender" => $g["Gender"], "Mobile" => $g["Telephone"], "Price" => $g["Price"], "PickupTime" => $g["arrival_time"]);
}

答案 1 :(得分:0)

问题是每次循环时都会覆盖响应数组。

所以你应该解决这个问题:

     $response[] = array("status" => 1 "Surname" => $g['fname'], "LastName" => $g["lname"],  "CarType" => $g["Car_Type"], "NumberPlate" => $g["Number_plate"], "CarImage" => $g["car_image"], "Gender" => $g["Gender"], "Mobile" => $g["Telephone"], "Price" => $g["Price"], "PickupTime" => $g["arrival_time"]);

修改

是的,当然关键$“Surname”不正确。你也应该改变它