Jump to content

Archived

This topic is now archived and is closed to further replies.

loganbest

getting stuff from MySQL DB and looping a display

Recommended Posts

I have been trying to get stuff to display on a page in a certain order from a DB. I have the connection done and keep meesing up the queries and the display looping.

Table name: magazines
Column: issue_id
Column: title
Column: num_of_articles

I need it to display in order of Issue ID


Can anyone help?

Share this post


Link to post
Share on other sites
The basic query you're looking for is;
select * from magazines order by issue_id asc
or
select * from magazines order by issue_id desc

The first will order the results on issue_id is ascending order, the second in descending order.

Share this post


Link to post
Share on other sites
Yea I realized what I was doing wrong about 10 minutes after I posted this. For anyone else who had this similar question, here is my code:
[code]total magazines created:
   <b> <? db_connect();  
$result=mysql_query("SELECT * FROM magazines");
       $num_rows = mysql_num_rows($result);
       echo $num_rows;
       if ($num_rows == "0"){
             
       echo '<h1>There are no Magazines!</h1><br><br>';
      
      } else if (!$num_rows == "0"){
    //MySQL Query to get the data
       $sql = mysql_query("SELECT * FROM magazines ORDER BY issue_id ASC") or die (mysql_error());
       //prints the table
       echo '<br><br><table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td><b>Magazine Name</b></td>
        <td width="100"><b># of Articles</b></td>
    </tr>';
       // keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($sql)) {

// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['title'];
echo "</td><td>";
echo $row['num_of_art'];
echo "</td></tr>";
}
echo "</table>";
      }
       ?> </b>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.