Jump to content

getting stuff from MySQL DB and looping a display


loganbest

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?
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.