0

I seem to be having a problem getting this while() loop to work...

include("database_connect.php");

$query = "SELECT id, title, date FROM entries";
$result = mysql_query($query);
				
while ( $result = mysql_fetch_assoc($result) ) {
				
	$id = $result["id"];
	$title = $result["title"];
	$date = $result["date"];
					
	echo "<div class=\"recent_entries\">";
	echo "<a href=\"archives.php?id=" . $id . "\">" . $title . "</a><br>";
	echo "<div class=\"date_line\">Posted on " . $date . "</div>";
	echo "</div>";
					
}

I have checked the database connection and it is working, the SQL statement is correct (tested in phpMyAdmin), and all of the field names from the associative array are correct.

There are two records in the database, and mysql_num_rows($result) returns both records.

The while() loop is returning only the first row, and I need it to return multiple rows...

Any help would be fantastic!!

2
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by chrisbliss18
0

Look at what you are doing in the following line:

while ( $result = mysql_fetch_assoc($result) )

You are using the $result variable for two different purposes, first it holds the query, then the results of the query. After the first loop iteration, you are passing the returned values back into the fetch function rather than passing in the query reference.

0

That has never been a problem before. However, changing the variable has helped. What would cause this to act differently this one time?

0

With the way that you are using it there, there isn't any way it would work properly. My only guess is that all the previous times you have used the variable that way, you were only retrieving one record.

Think about it in simple terms, you are essentially saying this:

$variable = 3;

while($variable == 3)
{
	$variable = 5;
}

There is no way for that loop to loop more than once, no matter how you look at it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.