2012-01-04 240 views
1

我想从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>&nbsp;</p> 
<p>&nbsp;</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"); 

回答

1

在你的'where'子句中使用datediff。这看起来像

$result = mysql_query(
     "SELECT userid, forename, surname, subscriptionexpiration 
     FROM {$table} 
     WHERE subscriptionexpiration > now() and 
      datediff(month, now(), subscriptionexpiration) <= -2 ORDER BY userid DESC"); 
+0

嗨,抽空非常感谢答复。我试过你的代码,不幸的是我收到以下错误: 警告: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

+0

嗨,请在本页末尾看到我的评论。我一直在看我的表结构,并意识到这个字段被设置为varchar而不是日期。我不确定这是否会造成问题。亲切的问候 – IRHM 2012-01-04 17:09:46

+0

不知道这是否是你的具体问题,但在datediff(并检查订阅日期是否大于当前日期),你需要一个日期(或smalldatetime)。您可以更改该列,也可以将其转换为smalldatetime。 – Kyra 2012-01-04 17:25:19

1

您的SQL似乎是倒退。您要求从现在开始的2个月到现在您不会找到订阅的所有行。应该从现在到现在2个月之间。在dateA和dateB之间使用时,总是以最早的日期开始

您可能需要在日期字段以及dateA和dateB之间进行一些转换以使所有内容匹配。

1

尝试此查询 -

SELECT 
    userid, forename, surname, subscriptionexpiration 
FROM 
    table_name 
WHERE 
    STR_TO_DATE(subscriptionexpiration, '%d%m%y') BETWEEN NOW() - INTERVAL 2 MONTH AND NOW() 
ORDER BY 
    userid DESC 
+0

非常感谢您花时间帮助我解决这个问题。我已经尝试过这些代码,虽然我没有收到任何错误,但它并未检索到期日期为011011的一条记录,即2011年10月1日,因此它应该显示在报告中。亲切的问候 – IRHM 2012-01-04 17:03:15

+0

嗨,我刚刚在看我的表结构,并意识到这个领域被设置为varchar而不是日期。你能告诉我,请问这有什么区别?亲切的问候 – IRHM 2012-01-04 17:08:20

+0

我认为这个领域最好使用DATE类型。它会帮助你避免格式问题。谁知道,是'ddmmyy'还是'yymmdd'? – Devart 2012-01-04 17:24:12

相关问题