我想从mySQL数据库中提取数据,该数据库显示订阅日期已过期2个月或更多的成员从当前日期。即我运行PHP报告的那一天。从今天的日期检索MySQL数据大于2个月
我试过使用下面的查询从我的数据库中提取数据,但它没有提取任何数据。
$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration BETWEEN DATE_SUB(now(), INTERVAL 2 MONTH) AND now() ORDER BY userid DESC");
日期存储在我的数据库为日月年,所以我不知道这是否是问题,还是我的查询是不正确。
完整剧本
<?php
$db_host = 'hostname';
$db_user = 'username';
$db_pwd = 'password';
$database = 'databasename';
$table = 'userdetails';
// use the same name as SQL table
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
function sql_safe($s)
{ if (get_magic_quotes_gpc())
$s = stripslashes($s);
return mysql_real_escape_string($s);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_POST["submit"]))
{
if (isset($_POST['del']))
$userid = intval($_POST['del']);
if (mysql_query("DELETE FROM {$table} WHERE userid={$userid}"))
$msg = 'The member who you selected has now been deleted!';
else
$msg = 'Could not delete the selected member';
}
}
?>
<html><head>
<title>Members With 2 Month Subscription Expiration</title>
<style type="text/css">
<!--
.style1 {
color: #FFFFFF;
font-size: 18px;
font-family: Calibri;
}
.style2 {
font-size: 18px;
font-weight: bold;
font-family: Calibri;
}
.style3 {font-family: Calibri}
.style6 {font-size: 16px}
.style7 {font-family: Calibri; font-size: 16px; }
-->
</style>
</head>
<body>
<p> </p>
<p> </p>
<p class="style7">
<?php
if (isset($msg)) // this is special section for
// outputing message
{
?></p>
<p class="style7">
<?=$msg?>
</p>
<?php
}
?>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">
<?php
$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration > now() and datediff(month, now(), subscriptionexpiration) >= 2 ORDER BY userid DESC"); if (mysql_num_rows($result) == 0) // table is empty
echo 'There are currently no members where their "Subscription Date" is greater than two months!';
else
{
echo "<table>\n";
while(list($userid, $forename, $surname, $subscriptionexpiration) = mysql_fetch_row($result))
{
echo "<tr>\n"
."<td><input type='radio' name='del' forename, surname value='{$userid}' /></td>\n"
."<td><small>{$forename} {$surname}</small><td>\n"
."<td><small>{$subscriptionexpiration}</small><td>\n"
."</tr>\n";
}
echo "<tr><td colspan=\"3\">";
echo '<input type="submit" value="Delete Selected Member" name="submit"/>';
echo "</td></tr>";
echo '</table>';
}
?>
<input type="hidden" name="action" id="action" />
</form>
</body>
</html>
解决方案
("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration <NOW() - INTERVAL 2 MONTH ORDER BY userid DESC");
嗨,抽空非常感谢答复。我试过你的代码,不幸的是我收到以下错误: 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源,位于/homepages/2/d333603417/htdocs/development/memberlist7.php上,线77 在我的代码中是直接在查询下面的那一行: if(mysql_num_rows($ result)== 0)// table是空的。 有什么想法吗?亲切的问候 – IRHM 2012-01-04 17:01:29
嗨,请在本页末尾看到我的评论。我一直在看我的表结构,并意识到这个字段被设置为varchar而不是日期。我不确定这是否会造成问题。亲切的问候 – IRHM 2012-01-04 17:09:46
不知道这是否是你的具体问题,但在datediff(并检查订阅日期是否大于当前日期),你需要一个日期(或smalldatetime)。您可以更改该列,也可以将其转换为smalldatetime。 – Kyra 2012-01-04 17:25:19