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); }
`
Related
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
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
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
I'm writing a simple message board where you can reply to any thread and then reply to any reply and so on.... everything works well but is there a simple method to loop the query as this could potentiality go on and on and on
$rsql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC";
$result = runSQL($rsql);
while ($row = mysql_fetch_array($result)) {
echo "".$row[title]."";
$rsql2 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID]' ORDER BY dateTime DESC";
$result2 = runSQL($rsql2);
while ($row2 = mysql_fetch_array($result2)) {
echo "".$row2[title]."";
$rsql3 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
$result3 = runSQL($rsql2);
while ($row3 = mysql_fetch_array($result3)) {
echo "".$row3[title]."";
}
mysql_free_result($result3);
}
mysql_free_result($result2);
}
mysql_free_result($result);
You could try UNION, probably not the best solution but it will work
$sql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC
UNION
SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID] ORDER BY dateTime DESC
UNION
SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
A better idea would be to re-design your database table structure.
I am trying to show all of the data in the 'status' column of my table but am having troubles. What am I doing wrong:
<?php
$query1 = "SELECT id, status FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1);
while ($row = mysql_fetch_array($result))
{
echo $row['status'] ;
}
?>
Try this:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}
Also, make sure that:
$_SESSION['customerid'], $start and $limit are not empty. You can test the constructed query with echo $query1;
Note: Addition of mysql_error() in in the mysql_query will allow you to see if there is an error in the query.
I am trying to show all of the data in
the 'status' column of my table
If you want to show all the rows, your query should be:
$query1 = "SELECT id, `status` FROM alerts ORDER BY id";
But if you want to show for a specific customer, your query should be:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id";