简体   繁体   中英

PHP SQL query to sum up all the completed tasks of the day

I am trying to develop a basic PHP code which will connect to the database, select the table "a" and check all the entries for the last 24 hours. If the value "b" of the entries is > 3 the query will understand them as completed, summarize them and echo the number of the completed ones.

This is what i have so far:

<?php
$con=mysql_connect("details hidden for security"); 
if (mysql_connect_errno($con)) { 
echo "Failed to connect to MySQL: " . mysql_connect_error(); 
 } 
$now = time();
$onedayago = $now - (24*60*60);
$result=mysqli_query($con, "SELECT * FROM 'a' WHERE 'b' = 3 AND 'b' >= $onedayago AND 'b' <= $now");
if($result === FALSE) 
{
die(mysql_error());
}
while($data=mysql_fetch_array($result)){ 
$count = $data['total']; 
}
 echo $count;
?>

In your code, you're using mysql_connect() to connect to the database, but after that you're using mysqli_query() to execute the query. Do not mix these.

Secondly, you do not need single quotes around the identifiers. It should be:

SELECT * FROM a WHERE b = 3 AND b >= $onedayago AND b <= $now

Also, mysql() is officially deprecated as of PHP 5.5 and it's recommended to switch to mysqli() or PDO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM