我有一项任务,我整天都在绞尽脑汁无济于事。我有一个MySQL表,其设置如下:
CREATE TABLE `Shows` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`band` varchar(255) NOT NULL,
`month` int(2) NOT NULL,
`day` int(2) NOT NULL,
`year` int(4) NOT NULL,
`venue` varchar(255) NOT NULL DEFAULT '',
`city` varchar(255) NOT NULL DEFAULT '',
`state` varchar(2) NOT NULL DEFAULT '',
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
基本上,这是一张保存音乐会信息的表格。月份和日期以数字形式存储而不带前导零(例如,2月3日将在月份列中表示为2,在日期列中表示为3)。
我正在创建一个活动日历页面,显示有关即将举行的音乐会的信息。我想设置它以便它在当前日期等待或之后的每一天循环播放,以便之前的音乐会不会出现在应该即将发生的事件上。
这是我到目前为止的代码:
function displayUpcomingShows()
{
include "/path/to/config/file/that/contains/credentials";
$con = mysql_connect($credentials);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$dateQuery = mysql_query("SELECT * FROM Shows GROUP BY month, day, year") or die(mysql_error());
$todayMonth = date(n);
$todayDate = date(j);
$todayYear = date(Y);
while ($info = mysql_fetch_array($dateQuery))
{
if ($todayMonth <= $info['month'] && $todayDate <= $info['day'] && $todayYear <= $info['year'])
{
$showDate = date("F j, Y", mktime(0, 0, 0, $info['month'], $info['day'], $info['year']));
echo "<b><li>$showDate</li></b>";
$concertDetails = $info['band'] . " @ " . $info['venue'] . " in " . $info['city'] . ", " . $info['state'];
echo "<li>$concertDetails</li>";
}
}
mysql_close($con);
}
如果有2个不同的日期,此代码可以正常运行。也就是说,如果插入表中的行如下所示:
INSERT INTO `Shows` (`id`, `band`, `month`, `day`, `year`, `venue`, `city`, `state`, `timestamp`)
VALUES
(1,'Some Band',12,8,2011,'The Crocodile Cafe','Seattle','WA','2011-12-07 22:50:06'),
(2,'Some Other Band',12,15,2011,'Nectar Lounge','Seattle','WA','2011-12-07 15:17:39');
网页显示:
December 8, 2011
Some Band @ The Crocodile Cafe in Seattle, WA
December 15, 2011
Some Other Band @ Nectar Lounge in Seattle, WA
完美,正是我想要的。但是,如果我改变了第一行,那么节目也在15日,就像这样:
(1,'Some Band',12,15,2011,'The Crocodile Cafe','Seattle','WA','2011-12-07 22:50:06')
网页只打印第一个条目,并且根本不显示第二个条目(Some Other Band),如下所示:
December 15, 2011
Some Band @ The Crocodile Cafe in Seattle, WA
有人能指出我正确的方向还是解释我做错了什么?我也尝试构造我的查询,以便它只选择不同的日期,循环使用相同的while循环和if语句在上面的代码,但然后在if语句中包含一个新的查询,从表中选择所有信息其中月,日和年匹配,然后使用另一个while循环来打印该数据,但这会导致页面在调用函数时停止加载。该代码如下所示:
function displayUpcomingShows()
{
(...same code as above up until here...)
$dateQuery = mysql_query("SELECT DISTINCT month, day, year FROM Shows") or die(mysql_error());
$todayMonth = date(n);
$todayDate = date(j);
$todayYear = date(Y);
while ($info = mysql_fetch_array($dateQuery))
{
if ($todayMonth <= $info['month'] && $todayDate <= $info['day'] && $todayYear <= $info['year'])
{
$showDate = date("F j, Y", mktime(0, 0, 0, $info['month'], $info['day'], $info['year']));
echo "<b><li>$showDate</li></b>";
$detailsQuery = mysql_query("SELECT * FROM Shows WHERE month = $info['month'] AND day = $info['day'] AND year = $info['year']") or die(mysql_error());
while ($concertInfo = mysql_fetch_array($detailsQuery)
{
$concertDetails = $concerinfo['band'] . " @ " . $concertInfo['venue'] . " in " . $concertInfo['city'] . ", " . $concertInfo['state'];
echo "<li>$concertDetails</li>";
}
}
}
mysql_close($con);
}
对不起,如果这个特别冗长,但我想确保尽可能详细。我在学习PHP / MySQL方面取得了不错的进展,但我想这就是为什么我还在苦苦挣扎。
欢呼任何帮助。
答案 0 :(得分:2)
轻松更新您的查询:
mysql_query(“SELECT * FROM显示WHERE时间戳&gt; ='”。日期('Y-m-d H:i:s')。'“)或死(mysql_error());
摆脱:
$ todayMonth = date(n); $ todayDate = date(j); $ todayYear = date(Y);
在你的while循环中一起摆脱IF语句
所以你的代码看起来像是:
function displayUpcomingShows()
{
include "/path/to/config/file/that/contains/credentials";
$con = mysql_connect($credentials);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$dateQuery = mysql_query("SELECT * FROM Shows WHERE timestamp >= '".date('Y-m-d H:i:s')."'") or die(mysql_error());
while ($info = mysql_fetch_array($dateQuery))
{
$showDate = date("F j, Y", mktime(0, 0, 0, $info['month'], $info['day'], $info['year']));
echo "<b><li>$showDate</li></b>";
$concertDetails = $info['band'] . " @ " . $info['venue'] . " in " . $info['city'] . ", " . $info['state'];
echo "<li>$concertDetails</li>";
}
mysql_close($con);
}
答案 1 :(得分:0)
你这里有一个解析错误
$detailsQuery = mysql_query("SELECT * FROM Shows WHERE month = $info['month'] AND day = $info['day'] AND year = $info['year']") or die(mysql_error());
它需要
$detailsQuery = mysql_query("SELECT * FROM Shows WHERE month = {$info['month']} AND day = {$info['day']} AND year = {$info['year']}") or die(mysql_error());
如果你想要的话,有很多关于字符串的信息,虽然它可能很难消化http://www.php.net/manual/en/language.types.string.php但这些例子应该是一个很好的参考。
帮自己一个忙,打开错误报告,这样php会告诉你这样的事情。找到你的php.ini并确保
display_errors = on
error_reporting = E_ALL
然后重新启动网络服务器以确保更改生效
尝试编码而不能看到详细的错误消息是地狱般的。
答案 2 :(得分:0)
问题在于您选择查询
SELECT * FROM显示GROUP BY月,日,年
GROUP BY导致问题。您基本上要求数据库合并数据。查询看到两条记录,但它们在同一天。您要求按天分组记录,因此它不会在同一天显示连续的两个记录。月和年也发生同样的事情。我认为你要做的是ORDER BY。
你真的应该使用像phpMyAdmin之类的东西来运行你的查询并在你的代码中插入错误之前解决它们。
答案 3 :(得分:0)
MySQL知道它是哪一天(CURRENT_DATE),因此您可以向数据库询问未来的节目,而无需在今天的日期查找代码。您也可以要求它为您格式化日期:
-- I prefer ANSI_QUOTES: using "identifier" vs. `identifier`
-- and PIPES_AS_CONCAT: a || b || c vs. CONCAT(a, b, c)
SET sql_mode='ANSI';
SELECT STR_TO_DATE("year"||','||"month"||','||"day", '%Y,%m,%d') AS showdate,
band,
venue,
city,
state
FROM Shows
WHERE "year" >= YEAR(CURRENT_DATE)
AND
"month" >= MONTH(CURRENT_DATE)
AND
"day" >= DAY(CURRENT_DATE);
请注意,如果将分解的日期字段折叠为单个列,则查询会更容易:
...
"showdate" DATE NOT NULL,
...
然后你可以简单地SELECT showdate, ... FROM Shows WHERE showdate >= CURRENT_DATE
答案 4 :(得分:-1)
感谢所有人的帮助。通过拼凑你的一些建议并纠正我忽略的一些错误,我终于能够使用以下代码获得我想要的结果:
$dateQuery = mysql_query("SELECT DISTINCT month, day, year FROM Shows") or die(mysql_error());
$todayMonth = date(n);
$todayDate = date(j);
$todayYear = date(Y);
while ($info = mysql_fetch_array($dateQuery))
{
if ($todayMonth <= $info['month'] && $todayDate <= $info['day'] && $todayYear <= $info['year'])
{
$showDate = date("F j, Y", mktime(0, 0, 0, $info['month'], $info['day'], $info['year']));
echo "<b><li>$showDate</li></b>";
$detailsQuery = mysql_query("SELECT * FROM Shows WHERE month = {$info['month']} AND day = {$info['day']} AND year = {$info['year']}") or die(mysql_error());
while ($concertInfo = mysql_fetch_array($detailsQuery))
{
$concertDetails = $concertInfo['band'] . " @ " . $concertInfo['venue'] . " in " . $concertInfo['city'] . ", " . $concertInfo['state'];
echo "<li>$concertDetails</li>";
}
}
}
这样只会成功打印每个唯一日期一次(if循环),然后打印与该日期相关的每个节目,然后再转到下一个日期,如下所示:
December 9, 2011
Some Band @ The Crocodile Cafe in Seattle, WA
Some Other Band @ Nectar Lounge in Seattle, WA
December 15, 2011
and so on and so forth
干杯!
肖恩