1

嘿伙计们,所以我在使用以下内容时遇到了一些麻烦:

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 的整数获取到当前日期并将其打印为整数时,它没有根据日期正确排序。

所以我需要在最旧的日期之前订购它,如下所示......

谢谢!

大卫

4

2 回答 2

3

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

... ORDER BY statuschangedate DESC
于 2013-04-16T01:22:01.930 回答
2

如果您想根据$timediff您的顺序进行排序,则应将 ORDER 子句更改为:

ORDER BY DATEDIFF(:date, statuschangedate)

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

于 2013-04-16T01:32:10.840 回答