Jump to content

Bit of help using foreach loops and mysql


AbydosGater

Recommended Posts

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
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]

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.