mitesh_gohil
mitesh_gohil

Reputation: 21

Previous record in MySql

In my table i have date records like 02-04-2016 , 03-01-2016 and 04-01-2016 if i am on 03-01-2016 i want the previous record which is 02-01-2016 But it gives me 01-01-2016 which is the first record of my table. No matter what date i am on.

if(isset($_POST['place'])){
$place      = $_POST['place'];
$date       = date("Y-m-d", strtotime($_POST['date']));
$classtype  = $_POST['classtype'];
$getdate    = mysql_query("SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' limit 0,1")or die(mysql_error());
$mydt       = mysql_fetch_array($getdate);

$mdt = date("d-m-Y", strtotime($mydt[classdate]));
echo $mdt;
}

Upvotes: 2

Views: 54

Answers (2)

Shailesh Katarmal
Shailesh Katarmal

Reputation: 2785

"SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' order by `classdate` desc limit 0,1"

Please use order by clause.

Upvotes: 1

Saharsh Shah
Saharsh Shah

Reputation: 29051

Use ORDER BY clause

Try this:

SELECT * 
FROM `class` 
WHERE `city`='$place' AND `clastype`='$classtype' AND 
      `classdate`<'$date' 
ORDER BY id DESC
LIMIT 0,1

Upvotes: 0

Related Questions