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'];}
Related
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;
}
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); }
`
I am trying to calculate the total of today's sales but this doesn't work.
It works, if I remove the date part...
# This works and will gives out the total:
$result = mysql_query("SELECT SUM(grand_total) AS value_sum FROM order");
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
# This doesn't work:
$result = mysql_query("SELECT SUM(grand_total) AS value_sum FROM new_order WHERE date = CURDATE()");
$query = mysql_query($result) or die ('Error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
Also, how can I get the monthly total of this column after this?
If date field is DATETIME type, then try this sql statement:
SELECT SUM(`grand_total`) AS value_sum
FROM `order`
WHERE DATE(`date`) = CURDATE()
I want to check if the current year/month/day/week/hour is the same as the dateTime saved in the db.
The current dateTime is: 2013-12-15 12:02:19
The db dateTime is: 2013-12-15 12:00:00
This is my query that checks the year/month/day/week:
$query_topics = mysql_query("SELECT * FROM topics WHERE day(startTime) = day(CURDATE()) and month(startTime) = month(CURDATE()) and day(startTime) = day(CURDATE()) and week(startTime) = week(CURDATE())") or die (mysql_error());
This one works but when I add hour to the query he will give me back null rows.
This is the query with year/month/day/week/hour:
$query_topics = mysql_query("SELECT * FROM topics WHERE day(startTime) = day(CURDATE()) and month(startTime) = month(CURDATE()) and day(startTime) = day(CURDATE()) and week(startTime) = week(CURDATE()) and hour(startTime) = hour(CURDATE())") or die (mysql_error());
Try replacing CURDATE() with NOW():
hour(NOW())
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'