How-to ORDER by DATE with this query - php

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")

Related

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); }
`

Previous record in MySql

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

How can i show Data from mysql specific month

$query = mysql_query(
"SELECT id, BuyerName,BuyerEmail,TransactionID,DateTime FROM `order`
WHERE DateTime="2015 AND 08""; LIMIT $start, $per_page"
)
or die(mysql_error());
I'm trying to make the query to show specific month and year.
You can use MySQL's Built-in YEAR and MONTH functions like:
SELECT `id`, `BuyerName`,`BuyerEmail`,`TransactionID`,`DateTime`
FROM `order` WHERE YEAR(DATE(`DateTime`))=2015 AND MONTH(DATE(`DateTime`)) = 8
If your column has a datatype of date you can use a range of dates for your criteria in a Sargable way
SELECT id, BuyerName,BuyerEmail,TransactionID,DateTime
FROM `order`
WHERE DateTime >='01-08-2015'
AND DateTime <= '31-08-2015'
For datetime you can write it as
SELECT id, BuyerName,BuyerEmail,TransactionID,DateTime
FROM `order`
WHERE DateTime >='01-08-2015 00:00:00'
AND DateTime <= '31-08-2015 23:59:59'
if type of column is datetime then try this
$query = mysql_query("
SELECT id, BuyerName, BuyerEmail, TransactionID, `DateTime`
FROM `order`
WHERE YEAR(DATE(`DateTime`)) = 2015 AND MONTH(DATE(`DateTime`)) = 8
LIMIT $start, $per_page
") or die(mysql_error());
or simply
$query = mysql_query("
SELECT id, BuyerName, BuyerEmail, TransactionID, `DateTime`
FROM `order`
WHERE `DateTime` LIKE '2015-08-%'
LIMIT $start, $per_page
") or die(mysql_error());
$time_you_want_to_choose = strtotime('previous month');
$ym = date('Y-m', $time_you_want_to_choose);
$query = mysql_query("
SELECT `id`, `BuyerName`, `BuyerEmail` , `TransactionID`, `DateTime`
FROM `order`
WHERE `DateTime` like '$ym%'
LIMIT $start, $per_page
");

MySQLi select from two table with limit

I have been try to combine two tables from mysql database, the two tables are status and status_reply both have the same columns number and name, that is id, account_name, author, postdate, data Please a help will be appreciated.
$limit = "LIMIT 0, 10";
$query = mysqli_query($db_conx, "(SELECT * `status` as type from status WHERE data LIKE '%".$tag."%' ORDER BY postdate DESC $limit)
UNION (SELECT * `status_reply` as type from status_reply WHERE data LIKE '%".$tag."%' ORDER BY postdate DESC $limit)");
//$query = mysqli_query($db_conx, "SELECT * FROM status WHERE data LIKE '%$tag%' ORDER BY postdate DESC $limit");
$statusnumrows = mysqli_num_rows($query);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$statusid = $row["id"];
$account_name = $row["account_name"];
$author = $row["author"];
$postdate = $row["postdate"];
$data = $row["data"];
$data = nl2br($data);
$data = str_replace("&","&",$data);
$data = stripslashes($data);
$statuslist .= '<div id="status_'.$statusid.'" class="status_boxes"><div><b>Ivotised by '.$author.' '.$postdate.':</b>
<article>'.$data.'</article>
</div></div>';
}
Use backquotes ` for field names instead of straight quotes '
Don't forget to quote $tag to protect from an SQL injection: mysqli_real_escape_string
Remember, that is you want to search literally for LIKE wildcard characters "%", "_" as well as backslash. You need to escape them too, using: $tag = addcslashes($tag, '\%_');
$limit = "LIMIT 0, 10";
$query = mysqli_query($db_conx, "
(SELECT `status` as type from status WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)
UNION
(SELECT `status_reply` as type from status_reply WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)");
I realized that I have to remove the type in both status and status_reply as references to the tables and identified each of the columns by their names. Am curious about it too!
$query = mysqli_query($db_conx, "
(SELECT id, account_name, author, postdate, data from status WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)
UNION
(SELECT id, account_name, author, postdate, data from status_reply WHERE data LIKE '%".$tag."%'
ORDER BY postdate DESC $limit)");

Use result of query in a second query

I'm trying to use the result of one query as part of a WHERE in a second query. I'm not sure how to best approach this, any assistance is greatly appreciated:
First Query:
$result = mysql_query("SELECT `keyword` FROM `history` ORDER BY `last_update` ASC LIMIT 1 ");
while($row = mysql_fetch_array($result))
$keyword = $row['keyword'];
Second Query
$result = mysql_query("SELECT `id` FROM `data_store` WHERE `keyword`= [result from my first query] ORDER BY `id` DESC LIMIT 1");
while($row = mysql_fetch_array($result))
$id = $row['id'];
For clarification, keyword.data_store relates to keyword.history
Use subquery
$result = mysql_query("
SELECT `id`
FROM `data_store`
WHERE `keyword` = (SELECT `keyword`
FROM `history`
ORDER BY `last_update` ASC
LIMIT 1)
ORDER BY `id` DESC");
while($row = mysql_fetch_array($result))
$id = $row['id'];
(Stripped out PHP aspects because it is irrelevant)
SELECT `id` FROM `data_store` WHERE
`keyword` IN (
SELECT `keyword` FROM `history` ORDER BY `last_update` ASC LIMIT 1
)
ORDER BY `id` DESC LIMIT 1
$result = mysql_query("SELECT `id`
FROM `data_store`
WHERE `keyword` = (
SELECT `keyword`
FROM `history`
ORDER BY `last_update` ASC
LIMIT 1 )
ORDER BY `id` DESC LIMIT 1");

Categories