gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:38:26 25 4
gpt4 key购买 nike

我想从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com