0

Why is this while loop showing the content two times before moving to the next entry? I've got double vision! Double entries instead of just a list of single entries. I'm using transient data with Wordpress by the way.

The output shows like this:

name1
name1
name2
name2
name3
name3

I'm trying to have it render like this:

name1
name2
name3

<?php

$result = mysql_query("SELECT * FROM wp_options WHERE option_name LIKE '%fparent_name_%' ORDER BY option_id DESC");

while ($row = mysql_fetch_array($result))
{
   $op_name =  $row['option_name']; // get the value of the option
   $op_time = substr($op_name, -10); // trim the option to the epoch time
   $the_name = get_transient('fparent_name_'.$op_time);  // get the name with epoch added

   $phptime = new DateTime("@$op_time");  // convert UNIX timestamp to PHP DateTime
   echo $the_name." - Submitted: ".$phptime->format('n/j/Y - g:ia'); // print the good stuff
?>
   <form name="more" action="" method="post" id="more">
      <input type="hidden" value="<?=$op_time;?>" name="timestamp" id="timestamp">
      <input type="submit" value="See Details" name="details" id="details">
   </form>
   <br>
<?php 
}
?>
Skatox
  • 4,237
  • 12
  • 42
  • 47
Tom
  • 3
  • 1
  • FYI, [you shouldn't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – What have you tried Mar 25 '13 at 18:51
  • 1
    I guess you didn't see my answer? – John Conde Mar 25 '13 at 19:01

1 Answers1

1

mysql_fetch_array() returns both an associative and numeric based array of your MySQL results. Use mysql_fetch_assoc() instead.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • After changing to mysql_fecth_assoc(), the loop still repeats two times before moving to the next item. Any idea what could be causing this? The data is NOT duplicated in the database. – Tom Mar 26 '13 at 21:00