SQL查询返回结果中只有一行

时间:2016-11-29 13:18:14

标签: php mysql json mysqli

我的查询结果只有一行。我需要得到三个结果,但它只显示结果中的一行。我试图尽可能多地更改代码,但结果中没有显示多行。我的代码如下所示。

elseif($module=="winners")
{

         $result = mysqli_query($conn,"SELECT * FROM `contest` WHERE showresult=1");
    if (mysqli_num_rows($result) > 0) {

         $result = mysqli_query($conn,"select * FROM `contest_score` order by score desc, CAST(timequiz as DECIMAL(9,2)) ASC limit 0,3");
    if (mysqli_num_rows($result) > 0) {

    $response["scores"] = array();

    while ($row = $result->fetch_assoc()) {
            $scores = array();
            $scores["id"] = $row["id"];
            $scores["user_id"] = $row["user_id"];
            $scores["username"] = $row["username"];
            $scores["score"] = $row["score"];
            $scores["played_date"] = $row["played_date"];
            $scores["timequiz"] = $row["timequiz"];
            $user_id = $row["user_id"];
            $result = mysqli_query($conn,"SELECT user_image FROM `users` WHERE id=$user_id");
            $temp_row = mysqli_fetch_assoc($result);

            $scores["user_image"] = $temp_row["user_image"];

          array_push($response["scores"], $scores);
          }
           $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);

如果有人可以帮助我解决问题,请检查并告诉我。

由于

1 个答案:

答案 0 :(得分:0)

这可能与它有关:您正在循环中重写$result变量。

$result = mysqli_query($conn,"SELECT user_image FROM 'users' WHERE id=$user_id");