php mysql select语句;没有得到输出

时间:2016-06-09 08:07:51

标签: php mysql

我想从数据库中输出从应用程序发送的总sms。

以这种方式:date 2016-06-09 | total sent 4 | delivered 3 | failed 2|

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

但是我在php中的代码不起作用。有人请带领我......

$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 个答案:

答案 0 :(得分:1)

将$ sql变量设为字符串: $ sql =“SELECT DATE(time_sent)AS日期,COUNT(*)as total_sent,SUM(status ='Success')已交付,SUM(状态='失败')AS失败FROM smsdb GROUP BY DATE(time_sent)”;

mysqli_query的第二个参数必须是字符串:http://php.net/manual/ro/mysqli.query.php

修改

你想要time_sent的值,但是在$ resultarr中你没有那个键,因为你已经在mysql查询中将它重命名为 date DATE(time_sent)AS date 即可。您的数组的键是:'date','total_sent','deliver'和'failed'。试试这个:$ time_sent = $ resultarr ['date'];