dongshi9407 2016-09-22 09:08
浏览 54
已采纳

如何在php预处理语句中显示值

I have the following query and I want to print the "total" I want to print the total like I print "num_rows" How can I do that ?

$stmt = $conn->prepare("SELECT SUM( total) AS total FROM money");
            $stmt->execute();
            $stmt->store_result();
            print $stmt->total;

my table structure are like this

id user monthly_pay total
1  Adam  1500       1500
2  Erik 1500       1500 
3  Mark  1500      1500
4 Gusta 1500       1500
5  Mike 1500       1500

I want to view the total "75000"

  • 写回答

2条回答 默认 最新

  • duanqiu9104 2016-09-22 09:45
    关注

    It's strictly not needed to use prepared statements when not dealing with user-input, but there's no disadvantage using it either (should you expand the query to deal with variables/user-input, the ground-work is already done!). This means for a static query like this, you can just use normal query() functions.

    However, using prepared statements like this, you need to bind the result and fetch it. Using $stmt->num_rows is going to trick you, as this will return the amount of rows returned by the query - which will only be 1. It will not return the result.

    The snippet below has been modified to use bind_result() and fetch(). This will prepare the query, bind the results to $sum and fetch it.

    $stmt = $conn->prepare("SELECT SUM( total) AS total FROM money");
    $stmt->execute();
    $stmt->bind_result($sum);
    $stmt->fetch();
    print "The sum is ".$sum;
    

    References

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制