Printing rows from a table, and the foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Printing rows from a table, and the foreach loop

Post by TheOracle »

Hi All,

I am trying to get some data from a table to display as hyperlinks within a html table, and am having some problems.

I'm getting the following error:

Code: Select all

invalid argument supplied for foreach()
Here is my code

Code: Select all

function writeList()
	{
	$result=mysql_query("select * from ws_basic order by site_name") or die(mysql_error());
	$list=mysql_fetch_array($result);
	echo "<tr>\n";
	echo "<td align='center'>Current Web Servers</td>\n";
	foreach($list as $record)
		{
		echo "<tr>\n";
		echo "<td><a href="ws_update_detail.php?siteid=$record[server_no]&".SID."">".html($record[site_name])."</a></td>\n";
		echo "</tr>\n";
		}
	}
Can someone suggest what is wrong, or a different way to approach it?

Many thanks
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

use
while($list = mysql_fetch_array($result))
instead of foreach.

And the $record[site_name] doesn't exist.
Use $list['site_name'];
Post Reply