Jump to content

Trying to figure out why this while loop isnt working


melting_dog

Recommended Posts

Hi guys

 

Probably a simple but trying to figure out why I cant get this while loop to work (at least I think its the loop)

 

Query works as it returns 1 row, but not all of them.

 

Any help would be great!

 

$sql = "SELECT * FROM requests ORDER BY leave_after DESC";

  if ($result = mysql_query($sql)) {

    if (mysql_num_rows($result)) {

 

 

  while ($row = mysql_fetch_assoc($result))

  {

echo '<div id="resultholder">';

echo "<p>Request ID:" . $row['id'] . ", User ID: " . $row['user_id'] . ", Departing From: " . $row['departing'] . ", Going To: " . $row['destination'] . " , Leaving After: " . $row['leave_after'] . " , but Arriving Before: " . $row['arrive_before'] ."</p></div>";

echo '</div>';

}

    }

  }

 

Thanks!

Link to comment
Share on other sites

I don't see any reason why all the records would not be displayed  = although I see you have two closing DIVs and only one opening div. But, i agree that the embedded conditions are a little odd. Are you positive there are more than 1 record in the table? Try the following:

 

$query = "SELECT * FROM requests ORDER BY leave_after DESC";
$result = mysql_query($query);

if (!$result)
{
    echo "Query failed: " . mysql_error();
}
else
{
    //Debug line
    echo "Rows returned: " mysql_num_rows($result) . "<br>\n";

    while ($row = mysql_fetch_assoc($result))
    {      
        echo "<div id='resultholder'><p>";
        echo "Request ID: {$row['id']}, ";
        echo "User ID: {$row['user_id']}, ";
        echo "Departing From: {$row['departing']}, ";
        echo "Going To: {$row['destination']}, ";
        echo "Leaving After: {$row['leave_after']}, ";
        echo "but Arriving Before: {$row['arrive_before']}"
        echo "</p></div>\n";
    }
}

Link to comment
Share on other sites

I'm going to guess that in your actual code, you are running another query inside of that loop and you are overwriting the $result variable so that when the loop condition is evaluated to start the second pass through the loop, $result no longer contains the result resource from the SELECT query.

Link to comment
Share on other sites

Hi guys,

 

Thanks for your help and sorry for the delayed reply. Yes, as it was the loop was working fine. The extra </div> was the spanner in the works hiding my results completely. Sorry - should have properly checked my code before posting.

 

Thanks guys!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.