[英]problem in query order by decending
我在mytable中有datetime(dt)有29行。 我想按降序对表进行排序,当我查询表时,其结果从28开始(未给出29),而28后则从1、2 ...... 27开始(其中(1-27 datetime是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."
"; // 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(..........
您正在从while循环中提取数组,因此在while循环中,您会从fetch_array中获得-1条记录。
我猜您正在使用GET ['dt']变量按日期进行过滤。
您可以直接在SQL查询中过滤记录。
像这样
SELECT * FROM table WHERE dt > {$_GET['dt']} ORDER BY dt DESC
当心传递正确的日期格式。
只需删除您的行$table = mysql_fetch_array($result1, MYSQL_ASSOC);
它会工作:
$dt1=$_GET['dt'];
$query="SELECT * FROM table ORDER BY dt DESC";
$result1 = mysql_query($query);
$dt2=$table['dt'];
echo $dt2."
"; // 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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.