user6394840
user6394840

Reputation: 25

While statement only echoing last result

I am very new, I am having a small problem.. I cannot get my while loop to loop through my entire result set, it is only retrieving the last result set, and i am expecting 2 result sets.

I echo'd my query out to see the result i would get, and the echo printed out both the result sets i want to print out. Leading me to the confusion my while loop is the issue.

I have looked through lost on here but the posts i have seen it was a problem with their query, rather then their while loop. Any assistance would be greatly appreciated. I have used different posts on here to construct my query, but i don't know where to go from here.

   date_default_timezone_set("Europe/London");

   $date = jddayofweek(unixtojd());

   $sql = "SELECT * FROM tbl WHERE ID = $ID AND Day = $date";

  $results = $conn->query($sql);
  echo $sql;

 if ($results->num_rows > 0) {
    while ($row = $results->fetch_assoc()) {

        $output = "Test2" . "</br>" . $row["time"] . "</br>";
    }


           } else {

    $output = $output . "test1" . "</br>";
    }
       }

Upvotes: 0

Views: 43

Answers (2)

Murad Hasan
Murad Hasan

Reputation: 9583

You are not echoing anything inside your while loop.

I think you need to concatenate the variable $output.

while ($row = $results->fetch_assoc()) {
    $output .= "Test2" . "</br>" . $row["time"] . "</br>";
}

Upvotes: 1

Miguel G. Flores
Miguel G. Flores

Reputation: 822

You are overwritting the content of $output on every iteration of the loop, you should use the concatenation operator to attach the value of the content to the end of the string.

$output .= "Test2" . "</br>" . $row["time"] . "</br>";

Upvotes: 0

Related Questions