SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot zeeshan's Avatar
    Join Date
    Mar 2001
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to loop through all rows in mySQL

    Hi all,

    I need to loop through all the rows in my SQL table and process each row individually. How can we do that?

    I tried "Select * from tablename" and then set up a for loop, but that simply gets the table. How can I go through each row of the table individually?

    Thanks.

  2. #2
    SitePoint Zealot theFinn's Avatar
    Join Date
    Oct 2000
    Location
    Edmonton
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm something like this should work:

    PHP Code:

    $sql 
    "SELECT * FROM mytable";

    if(!
    $result mysql_query($sql))
    {
       die(
    "Couldn't run query"):
    }

    while(
    $row mysql_fetch_array($result))
    {
      
    // Do what you want to $row, its now an array of the current row.
      
    echo $row['my_column'];

    That'll go though the results returned by the SELECT * and do something to each row... easy as 1 2 3.
    - theFinn
    Creator and Project Manager, phpBB
    http://www.theFinn.net
    http://www.tarotclass.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •