Jump to content

Archived

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

AbydosGater

Bit of help using foreach loops and mysql

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

Share this post


Link to post
Share on other sites
$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 data

Regards
Liam

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.