1

I am an amateur programmer and have recently been facing a challenge.

I am trying to select a data between range of dates but despite numerous attempts have been unsuccessful. Can someone help me with the code of pulling up data between date ranges.

My code is:

<?php


$tdate = $_POST['toDate'];
$fdate = $_POST['fromDate'];

    mysql_connect("localhost","user","pass") or die("Couldn't connect!");
    mysql_select_db("db_name") or die("Couldn't find db");

    $data = mysql_query("SELECT * FROM db_table BETWEEN saledate '$tdate' AND '$fdate' ");
    $result = mysql_fetch_array($data);

    if (!$result) {
    echo "No result";
    } else {
        echo $result;
    }
    ?>

3 Answers 3

3

You shouldn't do Query like that. Use PDO.

Regarding your SQL is wrong. The right is:

$data = mysql_query("SELECT * FROM db_table WHERE saledate  BETWEEN '$tdate' AND '$fdate' ");
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks yes123 that helps, since I am a really new programmer what command shall i use to display the entire data between these two dates on the page.
I am sorry i was unable to understand as I am not to that stage to understand the link. I am at a very basic level. However, if I am using mysql_num_rows function its showing me 2 and mysql_fetch_array it results array. Kinda confused..
@Darsh: you should force yourself to use PDO, it's simpler than it may look at first sight
i haven't started programming with objects in PHP. Shall look more into it. Thanks
2

You sql should be this

SELECT * FROM db_table WHERE saledate BETWEEN $tdate AND $fdate 

1 Comment

Thanks Ash that helps, since I am a really new programmer what command shall i use to display the entire data between these two dates on the page.
1

In your code, instead of

$result = mysql_fetch_array($data);
    if (!$result) {
    echo "No result";
    } else {
        echo $result;
    }

write the following code.

while($result=mysql_fetch_array($data)) 
{
echo $result['Fieldname1'];
......
......
echo $result['Fieldnamen'];
}

In place of fieldname, write the fields from your table. The fields you want to display.

1 Comment

i hope you know how to style it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.