簡體   English   中英

從 Mysql 表轉換時日期丟失位置

[英]Date loses position when translating from Mysql table

我的問題是,當我顯示我的 SQL 表時,日期的順序與 SQL 表上的順序不同,因此日期與 eid 不對應。

SQL 表具有以下順序。

EID 72:日期:2014-04-08 主題:第三次活動

EID 70:日期:2014-04-02 主題:第一個

EID 69:日期:2014-04-01 主題:第二次活動

EID 71:日期:2014-03-31 主題:第四次活動

但是它在 html 中顯示為

EID:71 日期:2014-04-08 主題:第三次活動

EID:72 日期:2014-04-01 主題:第四次活動

EID:69 日期:2014-04-02 主題:第一個

EID:70 日期:2014-03-31 主題:第二次活動

出於某種原因,當從 SQL 轉換為 PHP 時,日期失去了它的順序。 這是我的 php 代碼,它顯示了結果

$conn = mysql_connect("localhost","admin","password") or die ("could not connect to the server");
        mysql_select_db("app") or die ("that database could not be found");

        $result = mysql_query("SELECT * FROM  `notification` WHERE `date` AND `active`=0 < NOW()") or die ("The query could not be completed. Please try again later");

        while($noticerow=mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $nid[] = $noticerow['nid'];
            $eid[] = $noticerow['eid'];
            $subject[] = $noticerow['subject'];
            $noticedate[] = $noticerow['date'];
        }
        
        $a=0;
        while(mysql_num_rows($result)>$a)
        {
            echo "<tr><td><a onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>".$eid[$a]."</a></td><td><a onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>".$noticedate[$a]."</a></td><td><a onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>".$subject[$a]."</a></td><td><form action='rnotice.php' method='POST'><button value='submit' class='btn btn-danger pull-right' type='submit' onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>Remove</button><input name='eid' type='hidden' value=".$eid[$a]."></form></td></tr>";
        $a++;
        }

您需要在 SQL 語句中使用 order by 關鍵字,但我不知道您使用的列名稱。

假設您想按 EID 對它們進行排序,並且您有一個名為 EID 的列名,那么您的 select 語句應該是這樣的:

SELECT * FROM  `notification` WHERE `date` AND `active`=0 < NOW() order by eid ASC

如果你想按日期排序那么

SELECT * FROM  `notification` WHERE `date` AND `active`=0 < NOW() order by `date` ASC

要按降序對記錄進行排序,您可以使用 DESC 關鍵字而不是 ASC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM