query to get data for next available date if no data

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
peter162in
Forum Newbie
Posts: 12
Joined: Mon Jul 06, 2009 11:37 am

query to get data for next available date if no data

Post by peter162in »

Get data for a next available date from mysql database if data not available for the specified date.

Data arranged in table date wise. But for some date there is no data.
include "dbconect.php";

$date=$_GET['date'];

$sql = "SELECT * FROM table1 WHERE date='$date'";


$result = mysql_query($sql) or die(mysql_error());

if ( empty($result)) {
$dif2=strtotime ( '-1 day' , strtotime ( $date ) ) ;
$date2=date ( 'Y-m-d' , $dif2 ) . "<br />\n" ;

echo"$date2". "<br />";

}
$sql = "SELECT * FROM table1 WHERE date='$date2'";


I stuck ! any help how to proceed ahead? any way?


?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: query to get data for next available date if no data

Post by VladSun »

Please, use [ php ] [/ php] BBcode tags to surround your code.

You need a

Code: Select all

$row = mysql_fetch_assoc($result)
call in order to get the data.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply