PHP/MySQL cycling through rows

Discussion in 'PHP' started by MaxS, Dec 31, 2005.

  1. #1
    Hi,

    I have a few rows in a DB stored w/ a datetimecol. I am able to select one row by using this code:

    
          $sql = mysql_query("select title from news order by datetimecol desc limit 1");
          while ($row = mysql_fetch_assoc($sql))
          {
             echo "<br /><span style=\"background-color: blue\"><b>The newest news article is: $row[title] </b></span>";
          }
    PHP:
    But now I want to output the `title` of each row in descending order by the datetimecol ( newest being at the top ).

    How could I do this?
     
    MaxS, Dec 31, 2005 IP
  2. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Remove the "limit 1"
     
    tandac, Dec 31, 2005 IP
  3. MaxS

    MaxS Peon

    Messages:
    482
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks but I accidentally posted the wrong code!
          for($x = 0; $x<$result; $x++)
          {
            $sql = mysql_query("select title from news order by datetimecol desc");
            $row = mysql_fetch_assoc($sql);
            echo "$row[title] <br />";
          }
    PHP:
     
    MaxS, Dec 31, 2005 IP
  4. yo-yo

    yo-yo Well-Known Member

    Messages:
    4,619
    Likes Received:
    206
    Best Answers:
    0
    Trophy Points:
    185
    #4
    change this:
    
    $row = mysql_fetch_assoc($sql);        
    echo "$row[title] <br />";
    PHP:
    to this:
    
    while($row = mysql_fetch_array($sql))
    { echo "$row[title] <br />"; }
    
    PHP:
    :) This was one of the hardest things for me to learn when I first started with mySQL - you're on the right track!
     
    yo-yo, Dec 31, 2005 IP
  5. MaxS

    MaxS Peon

    Messages:
    482
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you very much, yo-yo, that's exactly what I needed. Cheers.
     
    MaxS, Dec 31, 2005 IP