Jimmy
Jimmy

Reputation: 31

Mysterious empty database entry

In the comment leaving system on part of my website there is a weird blank entry that always remains above the latest real entry. I've looked in the database directly and there isn't a blank entry. Yet, on the top of all my comments, there is always an empty box that is formatted the same way as the rest of the comments. Where is this coming from and how do I get rid of it? Here is the php:

$query = "SELECT * FROM catharsis";
$result = mysql_query($query);
$num = mysql_numrows($result);

mysql_close();
echo "<h4><center>Let it out.</center></h4>";


echo '<ul class="comments">';

for ($i = 0; $i < $num; $i++) {
    $name = mysql_result($result,$num - $i,"message");
    echo "<li>$name</li>";
    echo '<br>';
}
echo '</ul>';

relevant css:

ul.comments {
list-style-type: none;
position: relative;
right: 2.5em;

}

ul.comments li {
list-style-type: none;
background-color: c0dbff;
border-style: solid;
border-width: 1px;
border-color: black;
padding: .4em;
}

Upvotes: 0

Views: 60

Answers (3)

Robus
Robus

Reputation: 8269

$query = 'SELECT message FROM catharsis ORDER BY id DESC';
$result = mysql_query($query);
mysql_close();
echo '<h4><center>Let it out.</center></h4>';


echo '<ul class="comments">';

while($t = mysql_fetch_array($result)){
    echo '<li>'.$t[0].'</li><br>';
}
echo '</ul>';

Upvotes: 2

Josiah
Josiah

Reputation: 4854

I believe it comes from the first time this line is executed:

$name = mysql_result($result,$num - $i,"message");

Since your results are indexed from 0 to $num-1 you won't have an entry with the index $num-0. Change that line to read

$name = mysql_result($result,$num - $i -1,"message");

or start $i at 1 and let it run up to <= $num

Upvotes: 1

Matt Mitchell
Matt Mitchell

Reputation: 41823

Can you confirm that if you echo $num it is zero?

You don't want to be printing a ul when you don't have any li entries anyway so you'd want something like this:

if ($num > 0)
{
    echo '<ul class="comments">'; 

    for ($i = 0; $i < $num; $i++) { 
        $name = mysql_result($result,$num - $i,"message"); 
        echo "<li>$name</li>"; 
        echo '<br>'; 
    } 
    echo '</ul>'; 
 }

Upvotes: 0

Related Questions