0

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'];
?>
5
  • 4
    you're open to SQL injection and should address immediately Commented May 22, 2019 at 10:17
  • 1
    Your code is not correct with basic things, correct it first. Commented May 22, 2019 at 10:18
  • also - have you var_dumped $row and $row2 to make sure they're returning what you expect? Commented May 22, 2019 at 10:19
  • you need to pass $sql2 here $row2 = mysql_fetch_array($sql); Commented May 22, 2019 at 10:20
  • You need to make a call to mysqli_query($conn, $sql2). Commented May 22, 2019 at 10:22

2 Answers 2

1

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks it works!! but i change a little bit. thanks again!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.