douxing5199 2013-06-15 08:55
浏览 34
已采纳

在今天的日期计算MySQL中的SUM #Parse错误

<?php
while($row=mysql_fetch_array($res))
{
    //Result;
    //select the sum in here;
    $sumQry = "SELECT SUM(amount) FROM login WHERE tdate = '<?php echo date("F d, Y" ,time()); ?>"  />' ";
}
?>

ERROR Parse error: syntax error, unexpected T_STRING in ... on line 4

i want to calculate the sum of numbers ( i mean like 10+50+85+90) for today date and diplsay the sum in fount end. so used the above code. but i think some thing wrong with <?php echo date("F d, Y" ,time()); ?>


UPDATE Ok. Thanks guys! Leave about date. but i am not able to display the sum for total also.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ** on line 3

<?php
include "db.php";
while($row=mysql_fetch_array($res))
{
    //Result;
    //select the sum in here;
    $sumQry = "SELECT SUM(amount) FROM login";
}
?>
  • 写回答

2条回答 默认 最新

  • dongmacuo1193 2013-06-15 09:11
    关注

    I suggest you modify the MySQL table and use either DATE, DATETIME or TIMESTAMP.

    They way you do it your field is CHAR or VARCHAR, that means indexing even if present will not work properly.

    If you have the field as one of the above they can be indexed and performance of your query will be optimal even on a large number of records.

    Thing is DATE, DATETIME and TIMESTAMP are stored numerically by MySQL. Also you will be able to sort by this field if you ever need to show more then a record and want to order by this field.

    $sumQry = "SELECT SUM(`amount`) FROM `login` WHERE `tdate` = ". date("Y-m-d" ,time());
    $sumQry = "SELECT SUM(`amount`) FROM `login` WHERE `tdate` = ". date("Y-m-d H:i:s" ,time());
    $sumQry = "SELECT SUM(`amount`) FROM `login` WHERE `tdate` = ". time();
    

    I also added the MySQL fields quote because even if it's not necessarily most of the time, it is a good thing to use. If you have a large JOINed query it will perform up to 5% better with them. They are also good if you by any chance use reserved words as field names.

    Your code should look like this:

    include "db.php";
    $res = mysqli_query($con, "SELECT SUM(`amount`) FROM `login` WHERE `tdate` = '". date("Y-m-d" ,time() . "' GROUP BY `ammount`;");
    $row = mysqli_fetch_array($res);
    echo $row[1];
    

    In db.php you should have something like:

    $con = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作