Hello Dear all,

i have created a html page to insert data in sql table. my html page have date , month and year columns. which is stored in sql table in their respective columns.

now i want to retrieve data from sql table in a php page.

but i want to use a condition to select data.

and condition is that the data must be between two specify dates.

now i am confused how can i match 6 values in a query.

Example: Find the data between Day 02 Month March Year 2010 and Day 30 Month November year 2010.

because my sql table have different columns to store day , month and year.

please help me to solve it.

Thanks

Recommended Answers

All 4 Replies

You would need something like this

$date_1 = $_POST['date_1'];
$date_2 = $_POST['date_2'];

$query = 'SELECT * FROM table 
          WHERE date 
          BETWEEN '.$date_1.' AND '.$date_2;
$result = $mysql_query($query);

What this SQL query does, it selects all values from a table where the date is between date 1 and date 2.

If you wanted to limit the number of results which i', uncertain whether you said you require then you would need to add a LIMIT to the end of the query

$query = 'SELECT * FROM table 
          WHERE date 
          BETWEEN '.$date_1.' AND '.$date_2.' 
          LIMIT 6';

Obviously you would need to replace the table and date names with what you have in the database.

Just re-read your post

I assume you have the day, month and year in seperate dropdown boxes like many designers do for websites, if this is the case then read the following.


By storing the date in 3 separate columns you are making life hard for yourself.

I personally use just 1 column to store the Date and I use the DATE value in PHPMyadmin, now one problem with this is that it stores the date the American way like so yyyy-mm-dd which is not what you want if you live in the UK, It's much easier to simply format this date the way you want using the SQL DATE() function, google it.

When you insert the date into the database you must insert it using the American style by concatenating each of you day, month year columns.

eg: $date_1 = $_POST.'-'.$_POST.'-'.$_POST;

Thanks dear

it works.

But problem is that my months are stored as jan feb mar in table so when i run query it cant find any thing between dates

You must store months as the number alternative, storing the month as jan / feb etc doesn't allow you run this kind of query, store your months as numbers in the database.

When displaying the months to the user you can format them however you want.

I think you need to re-work the way you store your information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.