我的 mytable 中有 29 行,其中包含日期时间(dt)。我想按降序排列
当我查询表时,它给出从 28 开始的结果(未给出 29),在 28 之后,它从 1, 2....27 开始,其中
(1-27 日期时间为 0000-00-00 00:00:00),29 为最近时间。
$dt1=$_GET['dt'];
$query="SELECT * FROM table ORDER BY dt DESC";
$result1 = mysql_query($query);
$table = mysql_fetch_array($result1, MYSQL_ASSOC);
$dt2=$table['dt'];
echo $dt2."<br/>"; // if here i echo $table['id']; the result is here 29
if(strtotime($dt1) < strtotime($dt2))
{
while ($table = mysql_fetch_array($result1, MYSQL_ASSOC)){
echo $table['id']."<br />";
echo $table['name']."<br />";
}
}
else
echo "false";
为什么最后一个 id 没有显示,即 29
最佳答案
您将第一行读入第 3 行的 $table
$result1 = mysql_query($query);
因此,当进入 while 循环时,您将第二行读入 $table
,导致您错过第一行。
编辑: 您可以通过在开始 while 循环之前重置行指针来防止这种情况发生,如下所示:
mysql_data_seek($query,0);
while(..........
关于php - 查询顺序降序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5483848/