0

i have a question and hope for an answer.

now i want to retrieve some data from the database based on specific date.

So i have a table in the database called flights. this table stores the some air flights data in it.

I wrote a simple code to retrieve the flights through an interval: say i want to get the flights from day 24/11/2015 to 3/12/2015.

but the problem is when get the new month, the program crashes. i used a for loop so it keeps retrieving data forever.

how to increment date in php?

here is my code:

<?php

for($i = $date1; $i <= $date2; $i++)
{
$sql3 = "select * from flights where flight_date = '$i'";

$query3 = mysql_query($sql3);

$numrows3 = mysql_num_rows($query3);

if($numrows3 > 0)
{
 // retrieve flights.
}
} // end of for loop

?>
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
Hamada Hosny
  • 63
  • 2
  • 8
  • 4
    It would be far more efficient to let MySQL get all the rows you need rather than issue multiple queries, e.g. `select * from flights where flight_date between '2015-11-24' and '2015-12-03'` – Paul Dixon Nov 27 '15 at 20:27
  • And then you can get rid of the pesky `for` loop as well. ++2 for this approach. – Shawn Mehan Nov 27 '15 at 20:28
  • server not loving on you when you do it this way – Drew Nov 27 '15 at 20:29
  • Please also read this: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1 – miken32 Nov 27 '15 at 20:31
  • @PaulDixon thanks very much bro, it works. please write it in answer to accept it as solution for my question :) – Hamada Hosny Nov 27 '15 at 20:31
  • @Drew yes, it almost make server to down, but fortunately this is offline server so i just test this on it. – Hamada Hosny Nov 27 '15 at 20:33
  • Done :) I've also changed the title of your question, as the original wouldn't help anyone searching for this in future :) – Paul Dixon Nov 27 '15 at 20:34

1 Answers1

0

It would be far more efficient to let MySQL get all the rows you need rather than issue multiple queries, e.g.

select * from flights where flight_date between '2015-11-24' and '2015-12-03'

Just be sure to format the dates in your query correctly (yyyy-mm-dd). Also, please avoid mysql_ functions, they will be disappearing!

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • thank you so much, glad to help me :)... please vote up for question so no needs to get vote down as it is not an ambigious quesiton. – Hamada Hosny Nov 27 '15 at 20:39