Page 1 of 1

While not doing its job

Posted: Thu Jul 15, 2004 8:41 am
by pinehead18
I have my while loop setup in this script to display all the rows in the table. However, it only displays the first one. Any ideas?
Thank you
Anthony

Code: Select all

<?php
<?php

require('forum_head.php');
        $con = mysql_connect("localhost","pinehead","") or die(mysql_error());
        $db = mysql_select_db("lkcforum",$con) or die(mysql_error());
        $sql = "SELECT tid,uname,body,topic FROM threads WHERE tid='{$_GET['tid']}'";
        $result = mysql_query($sql,$con) or die(mysql_error());
        while ($row = mysql_fetch_array($result)) {
                
                $topic_name = $row['topicname'];
                $description = $row['topicdes'];
                $author = $row['uname'];
                $body = $row['body'];

        $db = mysql_select_db("pinehead",$con);
        $sql = "SELECT create_date FROM users WHERE user='anthony'";
        $result = mysql_query($sql,$con) or die(mysql_error());
        $row = mysql_fetch_array($result);
                $cdate = $row['create_date'];

$dis .= "
<table class="table2">
                <tr><td width=20% valign=top><font face=arial size=-1>Author</font></td><td width=80%
align=center>Message</td></tr>
                <tr><td width=20% valign=top><font face=arial size=-1>$author</font><br>
                                <font face=arial size=-1>Joined: $cdate</font><br>
                                 <a href=http://www..com/myprofile/$author/>View $author's profile</a>
                                <font face=arial size=-1>Posts: $posts<td width=80%>". nl2br($body) ."</font><br>
                </td>
                </tr>
                <tr>
                 <td><a href=post.php?tid=$tid>Reply</a></td>
                </tr>

</table>";


}


$output = ".$dis.";

start_header($output);

?>

Posted: Thu Jul 15, 2004 8:55 am
by hawleyjr
You have a few things wrong:

Code: Select all

<?php
        $sql = "SELECT tid,uname,body,topic FROM threads WHERE tid='{$_GET['tid']}'"; 
        $result = mysql_query($sql,$con) or die(mysql_error()); 
        while ($row = mysql_fetch_array($result)) { 
                
                $topic_name = $row['topicname']; 
                $description = $row['topicdes']; 
                $author = $row['uname']; 
                $body = $row['body']; 
?>
The $row[] array keys should be the same as the fields you are requesting in your select statement:

Code: Select all

<?php

 $sql = "SELECT tid,uname,body,topic FROM threads WHERE tid='{$_GET['tid']}'"; 

                $topic_name = $row['topic']; 
                $description = $row['tid']; 
                $author = $row['uname']; 
                $body = $row['body']; 
?>
Also change the second $row and $result variable names.

Code: Select all

<?php
        while ($row = mysql_fetch_array($result)) { 
                
                $topic_name = $row['topic']; 
                $description = $row['tid']; 
                $author = $row['uname']; 
                $body = $row['body']; 

        $db = mysql_select_db("pinehead",$con); 
        $sql = "SELECT create_date FROM users WHERE user='anthony'"; 
        $result2 = mysql_query($sql,$con) or die(mysql_error()); 
        $row2 = mysql_fetch_array($result2); 
                $cdate = $row2['create_date']; 
?>

Posted: Thu Jul 15, 2004 4:14 pm
by pinehead18
Thanks, it is working now