Displaying an entire MySQL table with PHP

Discussion in 'PHP' started by jawinn, Nov 19, 2006.

  1. #1
    How do I echo an entire MySQL table with PHP?

    Right now I'm using the following code:

    
    <?php
    // Make a MySQL Connection
    mysql_connect("server", "login", "password") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error());
    
    // Retrieve all the data from the "example" table
    $result = mysql_query("SELECT * FROM nfl_sch")
    or die(mysql_error());  
    
    // store the record of the "example" table into $row
    $row = mysql_fetch_array( $result );
    // Print out the contents of the entry 
    
    echo "week: ".$row['week']; 
    echo " Home Team: ".$row['team_h']; 
    echo " Away Team: ".$row['team_a']; 
    echo " Game Time: ".$row['time']; 
    echo " Home Team Votes: ".$row['v_team_h'];
    echo " Away Team Votes: ".$row['v_team_a'];
    
    ?>
    
    Code (markup):
    All I see is the first recode. There are about 5 and I want display all of them. Any help is much appreciated.
     
    jawinn, Nov 19, 2006 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    You have fetched only the first resulting row from mysql, but you need to fetch next rows too. See the manual for mysql_fetch_array, there is a simple example how to iterate through rows.
     
    wmtips, Nov 19, 2006 IP
  3. jawinn

    jawinn Active Member

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Now I see what I was doing wrong, thanks wmtips.
     
    jawinn, Nov 19, 2006 IP