35081

Mysterious empty database entry

Question:

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; }

Answer1:

$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>';

Answer2:

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

Answer3:

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>'; }

Recommend

  • Can anyone help me figure out what is wrong with this code?
  • Perl Moose accessors generated on the fly
  • d3js: time scaling and “1901”
  • Insert records into two table at once
  • Exporting SAS DataSet on to UNIX as a text file…with delimiter '~|~'
  • How would I send and receive packets over a WebSocket in Javascript
  • substitute period from abbreviation (single letter + period) unless followed by a capital letter
  • removing the default blue color on focus
  • Fraction length
  • Visual studio alerts workspace already exists
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Install PHP intl extension on MacOS
  • x64 applications using gdi+: what are the consequences on performance?
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Bad request using file_get_contents for PUT request in PHP
  • Refering to the class itself from within a class mehod in Objective C
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • MySQL WHERE-condition in procedure ignored
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • ORA-29908: missing primary invocation for ancillary operator
  • Do I've to free mysql result after storing it?
  • How to get next/previous record number?
  • php design question - will a Helper help here?
  • A cron job substitute?
  • json Serialization in asp
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • How do you join a server to an Active Directory (domain)?
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4