Previous record in MySql - php

In my table i have date records like 02-04-2016 , 03-01-2016 and 04-01-2016 if i am on 03-01-2016 i want the previous record which is 02-01-2016 But it gives me 01-01-2016 which is the first record of my table. No matter what date i am on.
if(isset($_POST['place'])){
$place = $_POST['place'];
$date = date("Y-m-d", strtotime($_POST['date']));
$classtype = $_POST['classtype'];
$getdate = mysql_query("SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' limit 0,1")or die(mysql_error());
$mydt = mysql_fetch_array($getdate);
$mdt = date("d-m-Y", strtotime($mydt[classdate]));
echo $mdt;
}

"SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' order by `classdate` desc limit 0,1"
Please use order by clause.

Use ORDER BY clause
Try this:
SELECT *
FROM `class`
WHERE `city`='$place' AND `clastype`='$classtype' AND
`classdate`<'$date'
ORDER BY id DESC
LIMIT 0,1

Related

How to query ORDER by date DESC when you have two dates in database?

My calendar table has start and end column and they have the same data type which is date. Now I'm having trouble on how to query and select the data in desc order according to the date recorded in the start column. Does anyone know how does my query should be?
I only have this:
//code indentation
$query = "select * from calendar";
$res = mysqli_query($conn,$query);
while($row=mysqli_fetch_array($res))
{
...
}
EDIT:
The right query is
$query = "select * from calendar ORDER by start DESC";
Thanks to everyone who answered! :)
Does just doing a simple ORDER BY not work?
$query = "select * from calendar ORDER BY start DESC";
$query = "select * from calendar ORDER BY start DESC";
$res = mysqli_query($conn, $query);
while( $row=mysqli_fetch_array($res) )
{
// your code
}
I think this should work. You can use ORDER BY.
$query = "select * from calendar ORDER BY date1, date2 DESC";
Mysql will first check for date1 if it is blank it will go for date2

MySQL/PHP - Display Recent Date

I was able to apply this line onto phpMyAdmin and it worked just fine.
SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1
The problem is that when I added the rest of the code, the recent date shows up blank on the webpage. Am I missing something in this code?
<?php
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
echo "$date";
?>
Any help is appreciated. Thank you.
. Try this
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY. date DESC LIMIT 1";
$result = mysql_query($query);
$r = mysql_fetch_assoc($result);
$date = $r['date'];
echo "$date";
You didn't set $date variable. You need to use mysql_fetch_array function for your $result variable.
Ex:
`
$query = "SELECT id, date_format('date', '%m.%d.%Y') as 'date' FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row); }
`

else mysql query to take up the first 20 dates

I have a sql query which has an if..else function. if the submit button is clicked, it will perform the particular query. else, the query will take in the first 20 dates in my database. however, i am not sure how to do the else query statement to take up only the first 20 dates. do help, thank you :)
<?php
$server = "localhost";
$user = "root";
$password = "";
$database = "sensors_database";
$connection = mysql_connect($server,$user,$password);
$db = mysql_select_db($database,$connection);
if(isset($_POST["usub"])){
$date1 = $_POST["datepicker"];
$date2 = $_POST["datepicker1"];
$query1 = "SELECT time, Ultrasonic FROM pi_sensors_network WHERE date BETWEEN '".$date1."' AND '".$date2."'";
$result1 = mysql_query($query1);
while($row = mysql_fetch_assoc($result1))
{
$dataset1[] = array(strtotime($row['time'])*1000,$row['Ultrasonic']);
//echo strtotime($row['time'])*1000;
}
}
else {
}
?>
Here you go:
SELECT `time`, `Ultrasonic` FROM `pi_sensors_network`
WHERE 1=1 ORDER BY `date` ASC LIMIT 0,20
If you are asking between the dates you mentioned in the query, then use
limit
$query1 = "SELECT time, Ultrasonic FROM pi_sensors_network WHERE date BETWEEN '".$date1."' AND '".$date2."' limit 0,20";
First Create index on table < pi_sensors_network > on column < date >
CREATE INDEX pi_sensors_network01 ON pi_sensors_network (`date`);
Then,
SELECT
`time`,`Ultrasonic`
FROM
pi_sensors_network
WHERE
`date` IN ( SELECT `date` FROM
(SELECT DISTINCT(`date`)
FROM pi_sensors_network
ORDER BY `date` ASC LIMIT 0,20)
AS tmp ) ;
If your table is too large, This query may take long even after indexing,
In this case,
I'ld suggest you to make an array of first 20 dates by separate query and then implode it in your main query !

Sort query by time and list next entries

I'm trying to sort this query by time.
I have a gaming match system. And I want to get a list of next 5 matches from my local time zone.
<?php
include_once "include/dbcompo.php";
$q=mysqli_query($con, "SELECT * FROM kamper ORDER BY tid LIMIT 5");
while($row = mysqli_fetch_array($q))
{
$clan1 = $row['clan1'];
$clan2 = $row['clan2'];
$server = $row['server'];
$tid = $row['tid'];
echo $clan1." ".$clan2." ".$server." ".$tid;
echo "<br />";
}
?>
Add a WHERE clause in your query: WHERE tid > NOW().
With NOW() you take the time of the server, maybe you should replace it with new DateTime(null)->getTimestamp()
Something like that:
<?php
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > NOW() ORDER BY tid LIMIT 5');
// or
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > '.new DateTime(null)->getTimestamp().' ORDER BY tid LIMIT 5');
?>
Two options:
1st option: Make the time field a numeric field and sort in PHP:
$queryResult = mysqli_query($con, $query);
while($row = mysqli_fetch_array($queryResult) {
$oldArray[$row['time'] = $row;
}
$array = ksort($oldArray);
foreach($array as $time=>$row) {
// do something
}
2nd option: Make a subquery
SELECT * FROM (
SELECT * FROM kamper WHERE timezone = 'UTC' ORDER BY tid
) LIMIT 5

How-to ORDER by DATE with this query

I am trying to change this query to order it by the date correctly.
This line works, but doesn't order all dates correct. It order by Year and not by days and months.
$query = mysql_query
("SELECT `date` FROM `table` WHERE `day` LIKE '%$row->period$row->friday%' AND date != '' ORDER BY `date` DESC LIMIT 1");
while($row2 = mysql_fetch_object($query)){
echo $row->date;
I created this, but the echo stays empty. What did I do wrong?
$query = mysql_query
("SELECT DATE_FORMAT(date, '%d-%m-%Y') as `date2` FROM `table` WHERE `day` LIKE '%$row->period$row->friday%' AND date != '' ORDER BY `date` ASC LIMIT 1");
while($row = mysql_fetch_object($query)){
echo $row->date2;
The syntax was wrong.
SELECT `date` FROM `table`
WHERE `day` LIKE '%".$row->period."%'
OR day like '%".$row->period."%' AND date != '' ORDER BY `date` DESC LIMIT 1")

Categories