How to list all rows in a mysql table

Discussion in 'PHP' started by Rinox, Dec 27, 2008.

  1. #1
    Hello guys! I'm wondering how I can list all of my rows from a mysql table in a html-table. I'm pretty sure I have to use a loop but I don't know how.
    In my mysql table I have two columns: link_name and link_url.

    I want a to list all of the links with link_name as the name of the link and link_url as the url of the link.

    I hope someone can help me with this :) Thanks!
     
    Rinox, Dec 27, 2008 IP
  2. izwanmad

    izwanmad Banned

    Messages:
    1,064
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $result = mysql_query("SELECT * FROM table_name");
    
    for($i = 0; $i < mysql_num_rows($result); $i++)
    {
    $row = mysql_fetch_array($result);
    echo '- <a href="' . $row['link_url'] . '">' . $row['link_url'] . '</a><br>';
    }
    PHP:
    hope it works... but, of course, you need a mysql connection above of these codes.
     
    izwanmad, Dec 27, 2008 IP
  3. Rinox

    Rinox Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! I guess I need to read some more about the for-loop :)
     
    Rinox, Dec 27, 2008 IP
  4. farad

    farad Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Another way to do this : while loop :rolleyes:
     
    farad, Dec 27, 2008 IP
  5. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Calculating on each iteration isn't good practice and may slow down the application.

    You might as well just use the simple way...

    <?php
    $sql = "SELECT Foo FROM Bar";
    $result = mysql_query($sql);
    
    if ( mysql_num_rows($result) > 0 )
    {
      while ( $row = mysql_fetch_assoc($result) )
      {
        echo('This row contains: '.$row['Foo'].'.<br/>');
      }
    }
    else
    {
      echo('Uhh, no results!');
    }
    
    PHP:
     
    chopsticks, Dec 27, 2008 IP