0

I want to out put from a database the total sms sent from an application.

In this manner: date 2016-06-09 | total sent 4 | delivered 3 | failed 2|

| date       | total_sent | delivered | failed |
+------------+------------+-----------+--------+
| 2016-06-09 |          4 |         3 |      1 |
+------------+------------+-----------+--------+

However my code in php does not work. Some one please lead me...

$sql = mysql_query("SELECT DATE(time_sent) AS date,COUNT(*) as total_sent,SUM(status = 'Success') as delivered,SUM(status = 'failed') AS failed FROM smsdb GROUP BY DATE(time_sent) ");

$result1 = mysqli_query($con, $sql);
$resultarr = mysqli_fetch_assoc($result1);
$time_sent = $resultarr['time_sent'];


echo $time_sent;   

1 Answers1

0

Make $sql variable a string: $sql = "SELECT DATE(time_sent) AS date,COUNT(*) as total_sent,SUM(status = 'Success') as delivered,SUM(status = 'failed') AS failed FROM smsdb GROUP BY DATE(time_sent) ";

The second parameter for mysqli_query must be a string : http://php.net/manual/ro/mysqli.query.php

EDIT

You want the value of time_sent, but in your $resultarr you don't have that key as you have renamed it to date in your mysql query: DATE(time_sent) AS date. The keys of your array are: 'date', 'total_sent', 'delivered' and 'failed'. Try this: $time_sent = $resultarr['date'];

mihaela
  • 394
  • 2
  • 12
  • Thank you for the help. However the select statement works as required, I have checked it from the command line. $result1 = mysqli_query($con, $sql); $resultarr = mysqli_fetch_assoc($result1); $time_sent = $resultarr['time_sent']; echo $time_sent; Is the part I don't understand how to frame it. Thank you –  Jun 09 '16 at 08:46
  • Hello there. When I try $time_sent = $resultarr['date']; ..... I only get the date and no other fields like total number __ delivered ___ or failed ___. Kindly... –  Jun 09 '16 at 09:25
  • Sorry, I think I have fixed it. Thank you @mihaela –  Jun 09 '16 at 09:28