如何使用php对数据库中的列求和

我正在尝试对列名“总计”求和。我想按 id 显示总排序。如果用户 A 登录,他可以在他的帐户中看到总预订量。

我不断收到错误:

“注意:Array 中的数组到字符串的转换。”

有人能帮我吗?我想以输入形式回显总数。

这是我的 php 代码:

<?php

    include ('connect.php');


    $sql = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";

    $result = mysqli_query($conn,$sql);

    $row = mysqli_fetch_assoc($result);

    $id = $row['p_id'];


    $sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";

    $row2 = mysql_fetch_array($sql);

    $sum = $row['total'];

?>


慕虎7371278
浏览 378回答 2
2回答

BIG阳

试试这个,$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";$result2 = mysql_query($sql2) or die(mysql_error());$row2 = mysql_fetch_array($result2) or die(mysql_error());$sum = $row['total'];

慕运维8079593

我知道了!谢谢这是我的代码这是代码:&nbsp; <?php&nbsp; &nbsp;include ('connect.php');&nbsp; &nbsp;$sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";&nbsp; &nbsp;$result8 = mysqli_query($conn,$sql8);&nbsp; &nbsp;$row8 = mysqli_fetch_assoc($result8);&nbsp; &nbsp;$id = $row8['p_id'];&nbsp; &nbsp;$sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";&nbsp; &nbsp;$result9 = mysqli_query($conn,$sql9);&nbsp; &nbsp;$row9 = mysqli_fetch_array($result9);&nbsp; &nbsp;$sum = $row9['total'];&nbsp; &nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP