i have 29 rows in mytable with datetime(dt). I want to sort it in descending order when i query the table it give the result starting from 28 (29 not given) and after 28 it starts from 1, 2....27 where (1-27 datetime is 0000-00-00 00:00:00) and 29 has the recent time.
$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";
Why the last id is not shown ie 29
You read your first line into $table
on line 3
$result1 = mysql_query($query);
So when entering your while loop you read the second row into $table
causing you to miss the first row.
EDIT : You can prevent this by resetting the rowpointer right before you start your while loop like this:
mysql_data_seek($query,0);
while(..........
you're doing fetch array out of while loop so in while loop you get -1 records from fetch_array.
As I am guessing you're doing the filter by date with GET['dt'] variable.
you can filter records directly in SQL query.
Like this
SELECT * FROM table WHERE dt > {$_GET['dt']} ORDER BY dt DESC
beware to pass the correct format of the date.
Just delete your line $table = mysql_fetch_array($result1, MYSQL_ASSOC);
and it will work :
$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";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.