jack98
jack98

Reputation: 69

How to sum column in database using php

i'm trying to sum a column name "total". and i want to display the total sorting by id. if user A login he can see total booking in his account.

I keep get the error:

"Notice: Array to string conversion in Array."

can someone help me? I want to echo the total in input form.

this is my php code:

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

Upvotes: 0

Views: 143

Answers (2)

jack98
jack98

Reputation: 69

i got it! thanks this is my code

this is the code:

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

Upvotes: 0

M.Hemant
M.Hemant

Reputation: 2355

Try this,

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

Upvotes: 1

Related Questions