Php 检索从今天起超过2个月的mySQL数据
我想从mySQL数据库中提取数据,该数据库显示订阅日期已超过当前日期2个月或更长时间的成员。i、 e.我运行PHP报告的那天 我尝试使用下面的查询从我的数据库中提取数据,但它没有提取任何数据Php 检索从今天起超过2个月的mySQL数据,php,mysql,Php,Mysql,我想从mySQL数据库中提取数据,该数据库显示订阅日期已超过当前日期2个月或更长时间的成员。i、 e.我运行PHP报告的那天 我尝试使用下面的查询从我的数据库中提取数据,但它没有提取任何数据 $result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration BETWEEN DATE_SUB(now(), INTERVA
$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> </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>
订阅到期2个月的会员
解决方案
("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} WHERE subscriptionexpiration <NOW() - INTERVAL 2 MONTH ORDER BY userid DESC");
(“从{$table}中选择userid、forename、姓氏、subscriptionexpntiation)其中SubscriptionExpatition您的sql似乎是向后的。您正在查询订阅从现在起2个月到现在为止过期的所有行,但您找不到这些行。它应该是从现在起到现在起2个月之间。在使用dateA和dateB之间时,始终从最早的日期开始
您可能需要对date字段以及dateA和dateB之间进行一些强制转换,以使所有内容匹配。在“where”子句中使用
$result = mysql_query(
"SELECT userid, forename, surname, subscriptionexpiration
FROM {$table}
WHERE subscriptionexpiration > now() and
datediff(month, now(), subscriptionexpiration) <= -2 ORDER BY userid DESC");
$result=mysql\u查询(
选择用户ID、姓名、姓氏、订阅支出
从{$table}
其中SubscriptionExpatition>now()和
datediff(month,now(),subscriptionExption)尝试此查询-
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
您好,非常感谢您花时间回复。我已经尝试了您的代码,但不幸的是我收到了以下错误:警告:mysql_num_rows():提供的参数不是有效的mysql结果资源,位于/homepages/2/d33603417/htdocs/development/memberlist7.php的第77行,在我的代码中是查询的正下方,即:if(mysql_num_rows($result)==0)//表为空。有什么想法吗?Kind regardsHi,请参阅本页末尾的我的评论。我一直在查看我的表结构,发现此字段设置为varchar而不是日期。我不确定这是否会造成问题。Kind regardsHi不确定这是否是您的特定问题,但在datediff中(并检查订阅日期是否大于当前日期)您需要一个日期(或smalldatetime)。您可以更改该列,也可以将其转换为smalldatetime。非常感谢。我想我需要去看看我的表结构,并可能对“订阅到期”字段进行一些更改。然后,考虑到您所说的内容,我将再次尝试。祝您好运:)希望这有帮助。嗨,非常感谢你花时间帮我解决这个问题。我尝试了代码,虽然没有收到任何错误,但它没有检索到过期日期为011011(即2011年10月1日)的一条记录,因此它应该显示在报告中。好心的regardsHi,我刚刚查看了我的表结构,意识到t此字段设置为varchar而不是日期。您能告诉我这会有什么不同吗?种类:我认为此字段最好使用日期类型。它将帮助您避免格式问题。谁知道,是“ddmmyy”还是“yymmdd”?@Devert,非常感谢您的时间和麻烦。而不是我占用您更多的时间时间到了,我要对我的桌子结构做些改变,我将从那里开始。再次致以衷心的祝贺和问候