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

时间:2019-05-22 10:17:09

标签: php database

我正在尝试对列名称“ total”求和。我想按ID显示总排序。如果用户为登录名,则他可以在其帐户中看到总预订。

我一直收到错误消息:

  

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

有人可以帮助我吗?我想在输入表单中回显总数。

这是我的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'];
?>

2 个答案:

答案 0 :(得分:1)

尝试一下

$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'];

答案 1 :(得分:0)

我明白了!谢谢,这是我的代码

这是代码:

  <?php

   include ('connect.php');

   $sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
   $result8 = mysqli_query($conn,$sql8);
   $row8 = mysqli_fetch_assoc($result8);
   $id = $row8['p_id'];

   $sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
   $result9 = mysqli_query($conn,$sql9);
   $row9 = mysqli_fetch_array($result9);
   $sum = $row9['total'];
   ?>