1

我对下面的代码有问题。

由于某种原因,它仅显示 10 月、11 月和 12 月的日期。

日期具有相同的格式......见下文:

DATE            RESULT

2013-02-01      Does Not Display
2013-03-01      Does Not Display
2013-04-01      Does Not Display
2013-05-01      Does Not Display
2013-06-01      Does Not Display
2013-07-01      Does Not Display
2013-08-01      Does Not Display
2013-09-01      Does Not Display
2013-10-01      Displays OK
2013-11-01      Displays OK
2013-12-01      Displays OK

下面是查询代码:

$events = array();
$query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date FROM mytable WHERE user_id = '$session_user_id' AND date LIKE '$year-$month%' AND active = 1";
$result = mysql_query($query,$db_link) or die('cannot get results!');
while($row = mysql_fetch_assoc($result)) {

$events[$row['date']][] = $row;

}

任何人都知道为什么会发生这种情况?

4

2 回答 2

2

我猜你正在使用月份 = 1。你只得到 10、11 和 12,因为它们是唯一以 1 开头的月份。

于 2013-02-01T13:51:41.757 回答
1

基于@mellamokb 的评论。而不是使用LIKEuse BETWEEN。这也使查询更快。

$query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date FROM mytable WHERE user_id = '$session_user_id' AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "' AND active = 1";
于 2013-02-01T13:52:16.870 回答