A simple bulletin-board system - view topic isn't working

A

Anonymous

Guest
I'm trying to build an extremely simply bulletin-board system, and it doesn't work. I've debugged it pretty well.
This is how it works: you post a message, and the Topics table has a Topic_id field that auto_increments. Then, the Replies table has a field, Topic_ref_id, which contains the Topic_id of it's parent topic. When the topic list page is generated, the link to follow into each topic contains a HTTP GET query string. The link generally looks like this
Code:
<a href="viewtopic.php?msg=<?php echo $Topic['Topic_id']; ?>"><?php echo $Message['Title_link_text']; ?></a>
Now, the script for viewtopic.php is where the problem is. It doesn't work, even though I have a row in the Replies table, with Topic_ref_id 1, and in the Topics table, I have a row with Topic_id 1.
It doesn't display the reply, for reason's unknown. Any thoughts?
Code:
<?php
   $M_Conn=mysql_connect ("localhost","root","some_pass");

   mysql_select_db ("forum",$M_Conn);

   $SQL="
         SELECT Title,Poster,Message,Topic_id
         FROM Topics
         WHERE Topic_id=" . $_GET['msg'] . ";
        ";

   $Result=mysql_query ($SQL,$M_Conn);

   $Message=mysql_fetch_array ($Result);
?>
<html>
<head>
   <title> View topic - <?php echo $Message['Title']; ?> </title>
</head>
<body>
   <i><h1> Topic: <?php echo $Message['Title']; ?> </h1>
   Posted by: <?php echo $Message['Poster']; ?>
   <br><br>
   Message:</i><br>
   <?php echo $Message['Message']; ?>
   <br><br>
<?php
   $SQL="
         SELECT Message,Poster,Topic_ref_id,Reply_id
         FROM Replies
         ORDER BY Reply_id DESC
         WHERE Topic_ref_id=". $Message['Topic_id'] .";
        ";
   $Result=mysql_query ($SQL,$M_Conn);

   while ( $Reply=mysql_fetch_array ($Result) )
   {
      ?>
      <i>Poster: <?php echo $Reply['Poster']; ?><br>
      Message:</i><br>
      <?php
      echo $Reply['Message'];
   }

   mysql_close ($M_Conn);
?>
</body>
</html>
 
The top works fine. I get a url viewtopic.php?msg=1. The $Topic associative array way fetched from the Topics table.
 
Oops, I realized the mistake - the ORDER BY and the WHERE clauses are in reverse - just need to swap em' around.
 
Zack said:
Oops, I realized the mistake - the ORDER BY and the WHERE clauses are in reverse - just need to swap em' around.

This is precicely why you should never write SQL queries in PHP. Always write them in a real MySQL client.
 
Um...what? What if I want my webpage (like above) to do a query... :-o
 
What I mean is that you should write all of your queries in a MySQL client first, so that you know that they work and so you can get meaningful error messages, and then once you've got them right, paste them into PHP.
 
Hrmm.. I usually only put it in the mysql client when something don't work.

echo $query;

COPY/PASTE/EXECUTE
 
Back
Top