Jump to content

More than 1 row to echo out?


zelig

Recommended Posts

For some reason, I can only get 1 row to echo out when there are actually multiple rows in the database that should be echoing.

 

Here is what I have:

 

	$result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");
	$post = mysql_fetch_assoc($result);
	echo stripslashes($post['message']) . "<br>\n" . " --- ".stripslashes($post['username']). " on ".stripslashes($post['time']) ."\n<hr width=90%>\n";

 

It pulls the one record great, but it only shows one record... I want to keep it to 10 records, thus the LIMIT in there (which I think I did right...), but it won't even show the ones in there right now (under 10, so that's not an issue yet).

Link to comment
https://forums.phpfreaks.com/topic/260351-more-than-1-row-to-echo-out/
Share on other sites

mysql_fetch_assoc() only pulls one record at a time from the result set. You will need a loop to get all the records. The manual for mysql_fetch_assoc() has an example script on how to do this.

 

http://php.net/manual/en/function.mysql-fetch-assoc.php

Hmm... I tried doing a loop, but I have to confess, I'm not overly familiar with them.

 

I got an error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

Here is the code I have now:

 

result = mysql_query("SELECT * FROM boards WHERE boardname='$board' ORDER BY id LIMIT 10");
	$post = mysql_fetch_assoc($result);
	while ($row = mysql_fetch_assoc($post)){
	echo stripslashes($row['message']) . "<br>\n" . " --- ".stripslashes($row['username']). " on ".stripslashes($row['time']) ."\n<hr width=90%>\n";}

 

What did I do wrong, so I can learn from it?

try

$query = "SELECT * FROM boards WHERE boardname = '$board' ORDER BY id LIMIT 10";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
  echo stripslashes($row['message']) . "<br>" . " --- ".stripslashes($row['username']). " on ".stripslashes($row['time']) ."<hr width=90%>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.