AbydosGater Posted September 23, 2006 Share Posted September 23, 2006 Hi, I know how to use a basic foreach loop.. ie:foreach ($item as $value){statment here};But how would i evolve this to work with rows in a database?say if i wanted to display info from my database rows in a list? what would i set the $item to?Could anyone help me please ive been trying to do this for weeks and just cant get it to work!Thanks Abydos Link to comment https://forums.phpfreaks.com/topic/21770-bit-of-help-using-foreach-loops-and-mysql/ Share on other sites More sharing options...
shocker-z Posted September 23, 2006 Share Posted September 23, 2006 $query=mysql_uery("SELECT * FROM table");while ($row=mysql_fetch_array($query) {//$row['ID'] would return the Id from that row};That's how most people loop thru mysql dataRegardsLiam Link to comment https://forums.phpfreaks.com/topic/21770-bit-of-help-using-foreach-loops-and-mysql/#findComment-97225 Share on other sites More sharing options...
Barand Posted September 23, 2006 Share Posted September 23, 2006 try[code]<?php$query = mysql_query("SELECT * FROM table");echo "<table border='1'>\n";while ($row=mysql_fetch_assoc($query)) { echo '<tr>'; foreach ($row as $field) { echo "<td>$field</td>"; } echo "</tr>\n";}echo "</table>\n";?>[/code] Link to comment https://forums.phpfreaks.com/topic/21770-bit-of-help-using-foreach-loops-and-mysql/#findComment-97228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.