Looping PHP/mySQL script

Hi every1 at sitepoint,

I have the following PHP script that opens my mySQL database takes the most recent article I’ve written and then echoes the article’s title, name of the author and the article itself.

This works fine. What I really want to do is for the script to loop and then retrieve and display the 2nd most recent article, then the 3rd, 4th etc. etc. as a long list of articles.

Your help would be greatly appreciated,

Peace, Leao


mysql_connect('mysql.com','user','pcode') or die(mysql_error());
mysql_select_db('my_cms') or die(mysql_error());
$result = mysql_query("SELECT * FROM article_db ORDER BY id DESC LIMIT 0, 1") or die(mysql_error());  
$row = mysql_fetch_array($result);
$myarticletitle=$row['myarticletitle'];
$myarticleauthor=$row['myarticleauthor'];
$myarticlecontent=$row['myarticlecontent'];

echo "<p><b>$myarticletitle</b>
<br />by $myarticleauthor</p>
<p><i>$myarticlecontent</i></p>"

Your’re right, I didn’t see that. Nice catch :slight_smile:


mysql_connect('mysql.com','user','pcode') or die(mysql_error());
mysql_select_db('my_cms') or die(mysql_error());
$result = mysql_query("SELECT * FROM article_db ORDER BY id DESC LIMIT 0, 1") or die(mysql_error());  
while($row = mysql_fetch_array($result))
{
  echo "<p><b>" . $row['myarticletitle'] . "</b>
  <br />by ".$row['myarticleauthor'] . "</p>
  <p><i>" . $row['myarticlecontent'] . "</i></p>";
}

:slight_smile:

Uh… you just need to tweak your query there slightly, Scallio - the query listed will pull 1 article only. (LIMIT 0, 1)