Jump to content


Bit of help using foreach loops and mysql

  • Please log in to reply
2 replies to this topic

#1 AbydosGater

  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 23 September 2006 - 12:39 PM

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

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#2 shocker-z

  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 23 September 2006 - 12:58 PM

$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

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 September 2006 - 01:48 PM


$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";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users