大家好,在使用以下代码时遇到了一些麻烦:

for ($i = 0; $i < count($noncompUsername); $i++)
{
    $tsql ="SELECT firstname,lastname,email,phone,statuschangedate FROM csvdata WHERE username = :username ORDER BY statuschangedate";
    $tgetmeminfo=$DBH->prepare($tsql);
    $tgetmeminfo->execute(array(':username' => $noncompUsername[$i]));
    while ($trow = $tgetmeminfo->fetch(PDO::FETCH_ASSOC)){

        $csvFirst = $trow['firstname'];
        $csvLast = $trow['lastname'];
        $csvEmail = $trow['email'];
        $csvPhone = $trow['phone'];
        $csvDate = $trow['statuschangedate'];
        $timediff = strtotime($date) - strtotime($csvDate);
        $timediff = floor($timediff/86400);

        $sql ="SELECT MailingAdrs FROM insuranceverificationdisclaimer WHERE TraineeUsername = :tusername";
        $getmeminfo=$DBH->prepare($sql);
        $getmeminfo->execute(array(':tusername' => $noncompUsername[$i]));
        while ($row = $getmeminfo->fetch(PDO::FETCH_ASSOC)){

            $csvAddrs = $row['MailingAdrs'];
            $change = 1;
        }
        if($change != 1)
        {
            $csvAddrs = "No address";
        }
        $change = 0;
        echo "$timediff, $csvFirst $csvLast, $csvEmail, $csvPhone, $csvAddrs";
    }
    echo "
    <br>
    ";
}

现在可以使用,但我要指出的部分是$tsql ="SELECT firstname,lastname,email,phone,statuschangedate FROM csvdata WHERE username = :username ORDER BY statuschangedate"; -现在,当我执行此操作并获取statuschangedate的整数作为当前日期并将其打印为整数时,它不会根据日期正确排序。

所以我需要按最旧的日期排序,如下所示...

谢谢!

大卫

===============>>#1 票数:3

按降序排列日期(使用DESC ):

... ORDER BY statuschangedate DESC

===============>>#2 票数:2 已采纳

如果要基于$timediff进行订购,则应将ORDER子句更改为此:

ORDER BY DATEDIFF(:date, statuschangedate)

当然,这实际上应该给出与您已经拥有的顺序相同的顺序,但是您至少可以使用此表达式在PHP本身中保存一些处理:)

  ask by David Biga translate from so

未解决问题?本站智能推荐: