mysql - while 循环运行两次?

标签 mysql wordpress loops while-loop

为什么这个 while 循环在移动到下一个条目之前显示内容两次?我有复视了!双重条目,而不仅仅是单个条目的列表。顺便说一下,我在 Wordpress 中使用 transient 数据。

输出显示如下:

name1
name1
name2
name2
name3
name3

我试图让它像这样渲染:

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

最佳答案

mysql_fetch_array() 返回 MySQL 结果的关联数组和基于数字的数组。使用mysql_fetch_assoc()相反。

Please, don't use mysql_* functions in new code 。它们不再维护and are officially deprecated 。请参阅red box ?了解 prepared statements相反,并使用 PDOMySQLi -this article将帮助您决定哪个。如果您选择 PDO,here is a good tutorial .

关于mysql - while 循环运行两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15622441/

相关文章:

php - PDO在登录时获取用户的数据库值并将其存储到$_SESSION

php - PDO SQLSRV 和 PDO MySQL 在获取 int 或 float 时返回字符串

MYsql 将 int 连接到 varchar (未按预期工作)

ruby - 如何在 ruby​​ 中编写负循环,如 for(i=index; i >= 0; i --)

mysql - 获取mysql中循环的信息

php - Wordpress Ajax 用于搜索结果 - 查询仅返回 1 个结果

html - Css 问题 : font-size

javascript - 如何在 WordPress 插件中处理 Ajax post 请求?

嵌套 if 语句中的 java.lang.ArrayIndexOutOfBoundsException 错误

c# - Visual C#,进程不断增长,直到出现与使用循环绘制图形的 UserControl 相关的内存错误