narendra narendra - 5 months ago 8
MySQL Question

How to get values from database from current date to last 5 days

Get the values from database dynamically. Here is my database

Database Image

And Here is my code

<form method="post" action="" align="center" style="margin-top:50px;">
<select id="date" name="date">
<option>Last 6 days</option>
<option>Last 2 days</option>
</select><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$date=$_POST['date'];
$con=mysql_connect("localhost","root","123");
$db=mysql_select_db("date");
$sql="select value from sort";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['value'];
}
}
?>


I want when i click on last 6 days shows current date to last 6 days values and when i click on last 4 days shows current date to last 4 days values. How to solve dynamically.

Answer

"select value from sort where (DATEDIFF(CURDATE(),date)<=6)" or

"select value from sort where  (DATEDIFF(CURDATE(),date)<=4)"

and so on...