Mysqli SUM query does not return result

Asked

Viewed 320 times

2

I am trying to sum up a column using the mysqli SUM method. The problem is that my query does not return results. My code is this:

   $sql = 'SELECT SUM(size) as soma, porta FROM `'
        .$this->options['db_table'].'` WHERE `porta`=?';
    $query = $this->db->prepare($sql);
    $query->bind_param('s', $file->porta);
    $query->execute();
    $query->bind_result(
        $soma
    ); 

   $query->close();

   echo $soma;

What could be wrong? Remembering that all other queries without using SUM() work perfectly.

1 answer

5


Lacked use fetch:

$sql   = 'SELECT SUM(size) as soma, porta FROM `'.$this->options['db_table'].'` WHERE `porta`=?';
$query = $db->prepare($sql);
$query->bind_param('s', $file->porta);
$query->execute();
$query->bind_result($soma); 

if($query->fetch()){
    echo 'result is ' . $soma;
}
$query->close();
  • 1

    Really! Inserting $query->fetch() worked, but I also had to change from "SUM(size) the sums, port" to just "SUM(size) the sums" and it turned out all right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.