sql query related issue - php

I have one more question in want to the user between the operator in php. This is my SQL query.
$results=$wpdb->get_results( "SELECT user_id,day,activity,hotel
FROM wp_user_activity
WHERE user_id = '$user_id' AND date BETWEEN $start AND $end ");
it gives me an error
$results=$wpdb->get_results( "SELECT user_id,day,activity,hotel
FROM wp_user_activity
WHERE user_id = '$user_id' AND date BETWEEN '26/05/2019' AND '31/05/2019' ");
am getting data in this why in the $start and $end am not getting any data

your date format is wrong
please use standard date format that is YYYY-MM-DD
Use this
$results=$wpdb->get_results( "SELECT user_id,day,activity,hotel
FROM wp_user_activity
WHERE user_id = '$user_id' AND date BETWEEN '2019-05-26' AND '2019-05-31' ");
Hope this will help you

try the following to see your SQL then review :
$sql = "SELECT user_id,day,activity,hotel FROM wp_user_activity WHERE user_id = '$user_id' AND date BETWEEN $start AND $end ";
echo $sql;
exit;

Now it is working. $arr=array(); foreach($results as $single) { $start = $single->trip_start_date; $end = $single->trip_end_date; /* return $sql = "SELECT user_id,day,activity,hotel FROM wp_user_activity WHERE user_id = '$user_id' AND date BETWEEN '$start' AND '$end' "; */ $results=$wpdb->get_results( "SELECT user_id,day,activity,hotel FROM wp_user_activity WHERE user_id = '$user_id' AND date BETWEEN '$start' AND '$end' "); $single->trip_details=$results; $arr[]=$single;
}

Related

Search data between two dates in dd/mm/yyyy hh:mm:ss in sql

I want to fetch all records between two dates from database in php. Date format is dd/mm/yyyy hh:mm:ss. example 06/Dec/2016 05:56:15
I'm using following code
$Sdate=date_create($_GET['sdate']);
$start=date_format($Sdate,"d/M/Y H:i:s");
$Edate=date_create($_GET['edate']);
$end=date_format($Edate,"d/M/Y H:i:s");
$sql = "SELECT * FROM `payments` WHERE `customerid` = '".$_SESSION['id']."' AND dateandtime BETWEEN ('".$start."', '".$end."') ORDER BY id DESC";
But this is not working
Thank You in advance for helping me
Try this:
$Sdate = date('Y-m-d H:i:s', strtotime($_GET['sdate']);
$Edate = date('Y-m-d H:i:s', strtotime($_GET['edate']);
$sql = "SELECT * FROM `payments`
WHERE `customerid` = '".$_SESSION['id']."'
AND dateandtime BETWEEN '$Sdate' AND '$Edate'
ORDER BY id DESC";
The correct syntax is:
dateField BETWEEN dateFieldLow AND dateFieldHigh

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

Sum table1 Between $date1 and $date2 AND username Equals $user

hello im having some problem with this sum select does anyone know whats wrong with this, it seems that im getting no result from it
$result = mysql_query("SELECT SUM(total)
FROM table1 where date BETWEEN '".$date1." 00:00:00' AND '".$date2." 23:59:59' and username = ".$user."");
while($row=mysql_fetch_array($result))
{ $sum = $row['SUM(total)'];}
Try
$query = "SELECT SUM(total) AS result
FROM table1
WHERE DATE(date) BETWEEN '$date1' AND '$date2'
AND username = '$user'";
$con = \\your db connection string
$result = mysql_query($con,$query);
while($row=mysql_fetch_array($result))
{$sum = $row['result'];}

SELECT SUM with date variable, for prestashop

i'd like to have a result on PHP/MYSQL
I have a table ps_orders with price on total_paid
I need to ask total for all price in current date, i have dificult ti insert correct date. I'm stopping here, and do not works... thnaks
....
$date = date("Y-m-d");
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '%$date%'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo mysql_result($result, 0);
Try
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '$date'";
or
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date LIKE '%$date%'";
depending on how your delivery_date field is setup as well as what you are using in your $date variable (time stamp vs just m-d-y), your query up to the where clause looks okay, but you could also try:
SELECT SUM(total_paid)
FROM ps_orders
WHERE delivery_date = $date;
if you are using a datetime field for delevery_date, you'll have to go more in depth and use a range:
SELECT SUM(total_paid)
FROM ps_orders
where (delivery_date > $date
and deliver_date < $date +interval 1 day)
This link should also help you out quite a bit when working with date: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
You can use one of the following:
DATE function in mysql converts 2013-11-20 10:54:12 to 2013-11-20, i.e. truncated a time in date
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE DATE(delivery_date) = '$date'";
or you use only one '%' after $date with using LIKE, so this value will be matched for dates like 2013-11-20 10:12:13 :
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date like '$date%'";
or use string mysql function SUBSTRING
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE SUBSTRING(delivery_date1, 1, 10) = '$date'";
use datetime with time and BETWEEN mysql comparison operator:
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date1 between '$date 00:00:00' and '$date 23:59:59'

Categories