简体   繁体   中英

Retrieve mySQL Data Greater Than 2 Months From Todays Date

I'm wanting to extract data from a mySQL database which shows the members with a subscription date which has expired 2 months or more from the current date. ie the day I run my PHP report.

I've tried using the query below to pull the data from my database, but it's not extracting any data.

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

The date is stored in my database as ddmmyy, so I'm not sure whether this is the issue or whether my query is incorrect.

Full Script

<?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>

Solution

("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration <NOW() - INTERVAL 2 MONTH ORDER BY userid DESC");

Your sql appears to be backwards. You are asking for all rows where the subscription expires between 2 months from now and now which you won't find. It should be between now and 2 months from now. Always start with the oldest date when using between dateA and dateB

You may need to do some casts on the date field and the between dateA and dateB to get everything to match up.

In your 'where' clause use datediff . This would look like

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

Try this query -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM