Use of BETWEEN function from MYSQL database
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 unsuccess开发者_JAVA技巧ful. 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;
}
?>
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' ");
You sql should be this
SELECT * FROM db_table WHERE saledate BETWEEN $tdate AND $fdate
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.
精彩评论