-2

我有一个名为 notify 的表,其中 (seeker,donor, date) 列

是类型 (datetime) 的日期列,它存储以下格式 YYYY-MM-DD HH:MM:SS

我正在尝试选择 1 条记录最新日期从通知表中,然后将日期与当前日期进行比较并计算两个日期之间的天数..

<?php

session_start();
$email = $_GET['email'];
date_default_timezone_set('Asia/Riyadh');
$time = date("Y-m-d H:i:s");

$note = "SELECT * FROM notify WHERE seeker='".$_SESSION['email']."'AND donor='".$email."' ORDER_BY `date` DESC LIMIT 1";
$st = $conn->prepare($note);
$st->execute();

if($found = $st->fetch(PDO::FETCH_ASSOC)){
    $now = $time;
    $old_date = strtotime($found['date']);
    $dateif = $now - $old_date;

    if(floor($dateif/(60*60*24)) >= 7){
    echo "the difference between tow dates is 7 days or more";
    } else { echo "difference between tow dates is less than 7 days";}
}
?>


代码不工作!
我的通知表中只有一条记录,日期为 2013-04-22 09:15:47

4

8 回答 8

4

首先,您应该使用这样的准备好的语句:

$note = "SELECT * 
    FROM notify 
    WHERE seeker=:seeker AND donor=:donor 
    ORDER BY `date` DESC
    LIMIT 1";

$st = $conn->prepare($note);
$st->execute(array(
    ':seeker' => $_SESSION['email'],
    ':donor' => $email,
);

如果没有占位符,您仍然可以接受 SQL 注入。

其次,您不能以这种方式将字符串与整数进行比较:

$now = $time; // string
$old_date = strtotime($found['date']); // integer
$dateif = $now - $old_date; // dunno?

您应该将苹果与苹果进行比较:

$seven_days_ago = strtotime('-7 days');
$old_date = strtotime($found['date']);

if ($old_date > $seven_days_ago) {
    echo "difference between tow dates is less than 7 days";
} else {
    echo "the difference between tow dates is 7 days or more";
}
于 2013-04-23T07:41:16.853 回答
1

由于您的date列不存在,因此按它排序没有意义。此外,在不安全的情况下,您会暴露于 SQL 注入$_SESSION['email']

因此,正确的形式是使用准备好的语句,以及按右列排序。(假设 PDO,您也可以使用 mysqli):

/** @var PDO $pdo - Assuming a PDO connection. */
$query = "SELECT * FROM `user` WHERE `ID` = :email ORDER BY `time` DESC";
$stmt = $pdo->prepare($query);
$stmt->execute(array($_SESSION['email']));

$result = $stmt->fetchAll(PDO::FETCH_ASSOC); //Get all results in an associated array form.
于 2013-04-23T05:38:10.897 回答
1

Jack 的回答向您展示了如何正确使用准备好的语句。这是使用DATEDIFF().

$note = "SELECT *, DATEDIFF(NOW(), `date`) AS date_diff
         FROM notify 
         WHERE seeker=:seeker AND donor=:donor
         ORDER_BY `date` DESC
         LIMIT 1";

$st = $conn->prepare($note);
$st->execute(array(
    ':seeker' => $_SESSION['email'],
    ':donor' => $email,
);

$row = $st->fetch(PDO::FETCH_ASSOC);
// do something with $row
于 2013-04-23T07:50:14.020 回答
0

如果您将任何变量附加到字符串,那么您需要使用点连接它们,并且 oder 将出现在 where 条件和 $_SESSION 内部您错过引号之后

$query = "SELECT * FROM user WHERE ID='".$_SESSION['email']."' ORDER_BY date, time";
于 2013-04-23T05:32:39.937 回答
0

要从数据库中检索最新日期,请尝试执行以下 sql 查询

$query="SELECT * FROM user WHERE ID='".mysql_real_escape_string($_SESSION[email])."' ORDER_BY date,time desc limit 1";
于 2013-04-23T05:40:40.937 回答
0

为了检索最新日期,您需要按降序对日期字段进行排序

  $note = "SELECT * FROM notify WHERE seeker=' ".$_SESSION['email']. " ' AND donor=' ".$email." ' ORDER_BY date DESC LIMIT 1";
于 2013-04-23T07:35:40.050 回答
0

你忘了`在这里日期。日期是mysql中的保留字,

如果您想将其用作列名,请在其周围放置`。

编辑

你也有多余的空间删除它

$note = "SELECT * FROM notify WHERE seeker='".$_SESSION['email']. "' 
AND donor='".$email."' ORDER_BY `date` LIMIT 1";
于 2013-04-23T07:29:49.147 回答
0

你试过按desc订购吗?如下图:

$note = "SELECT * FROM notify 
           WHERE
           seeker=' ".$_SESSION['email']. " ' 
           AND
           donor=' ".$email." ' ORDER_BY date DESC LIMIT 1";
于 2013-04-23T07:30:04.217 回答