ORDER_BY日期限制1 [重复]

This question already has an answer here:

I have table named notify with (seeker, donor, date) columns

the date column of type (datetime) and it stores the following format YYYY-MM-DD HH:MM:SS

I'm trying to SELECT 1 record with the latest date from notify table and then compare the date with the current date and calculate the number of days between tow dates..

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


the code is not working !
i have only one record in my notify table with this value in date 2013-04-22 09:15:47

</div>

First of all, you should use prepared statements like this:

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

Without the place holders you're still open to SQL injection.

Second, you can't compare a string with an integer in this way:

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

You should compare apples with apples:

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

If you are attching any variables to string then you need to concatinate them using dot and oder by will come after where condition and inside $_SESSION you missed quotes

$query = "SELECT * FROM user WHERE ID='".$_SESSION['email']."' ORDER_BY date, time";

Since your date column doesn't exist, there's no point in ordering by it. Also, you're exposed to SQL injection in the case where $_SESSION['email'] is not secured.

So, the correct form would be to use prepared statements, as well as order by the right column. (assuming PDO, you can use mysqli as well):

/** @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.

For retrieving latest date from database please try executing following sql query

$query="SELECT * FROM user WHERE ID='".mysql_real_escape_string($_SESSION[email])."' ORDER_BY date,time desc limit 1";

you forgot ` here around date. date is reserved word in mysql,

if you want to use it as column name place ` around it.

EDIT

also you have extra space remove it
$note = "SELECT * FROM notify WHERE seeker='".$_SESSION['email']. "' 
AND donor='".$email."' ORDER_BY `date` LIMIT 1";

have you try to order by desc? as shown bellow:

$note = "SELECT * FROM notify 
           WHERE
           seeker=' ".$_SESSION['email']. " ' 
           AND
           donor=' ".$email." ' ORDER_BY date DESC LIMIT 1";

In order to retrieve latest date you need to sort field for date in descending order

  $note = "SELECT * FROM notify WHERE seeker=' ".$_SESSION['email']. " ' AND donor=' ".$email." ' ORDER_BY date DESC LIMIT 1";

Jack's answer shows you how to use prepared statements correctly. Here is the code to simplify the date calculation using 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