php - 检索距今天日期超过 2 个月的 mySQL 数据

标签 php mysql

我想从 mySQL 数据库中提取数据,该数据库显示订阅日期从当前日期起已过期 2 个月或更长时间的成员。即我运行 PHP 报告的那一天。

我已经尝试使用下面的查询从我的数据库中提取数据,但它没有提取任何数据。

$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"); 

日期在我的数据库中存储为 ddmmyy,所以我不确定这是问题所在还是我的查询不正确。

完整脚本

<?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");

最佳答案

在“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");

关于php - 检索距今天日期超过 2 个月的 mySQL 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730182/

相关文章:

PHP foreach 行作为 $var,行名和行号重复

php - 手动将结果集插入查询

mysql - Doctrine 相同的查询,getResult 与 getArrayResult 返回不同的结果

php - 我确定这是一个快速修复,但它似乎不像这样工作

Mysql:如何查询类型为位的列?

mysql - 获取每组分组结果的前n条记录

java - mysql insert with java and php : no error but no insert

php - 我如何显示使用 PHP 和 MySQL 的网站上哪些日期和时间最活跃?

php - 使用php在openshift上连接数据库

java - 比较两个音频文件。声音匹配