无法回显查询结果

I have a class, where i have a function with the following query.

public function count_weight() {
    $id = $_SESSION['id'];
    $query = $this->db->prepare("SELECT sum( weight ) FROM `fish` WHERE `user_id` = '".$_SESSION['id']."'");

    try {
        $query->execute();
    } catch(PDOException $e) {
        die($e->getMessage());
    }

    return $query->fetchAll();
}

I want to echo out this result on my index-page. Right now it looks like this:

$weight = $users->count_weight();

echo $weight;

Which obviously doesn't work, it only prints out "arraykg".

Any suggestions?

You can't print an array like that. e.g.

$arr = array(1,2,3);
echo $arr;

is going output the literal text Array, because you're using the array in a string context. If you want to print the CONTENTS of the array, you'll need to do something with it, e.g.

echo implode(',', $arr); // prints: 1,2,3

or

print_r($array); // debug dump out of the array

Try this for example:

$weight = $users->count_weight();
foreach($weight as $w)
{
   echo($w['id']):
}

$query->fetchAll(); will return an array. Use var_dump() to analyze the array and adjust your variable declaration accordingly.

echo "<pre>";
print_r($weight);
echo "</pre>";

Assuming $weight actually contains data and is not a resource.

$weight is an array of the row that it retrieved. You'll need to echo out that column

echo $weight['weight'];

This may or may not work because of the query. Try changing the query to this:

$query = $this->db->prepare("SELECT sum( weight ) AS weight FROM `fish` WHERE `user_id` = '".$_SESSION['id']."'");