来自MySQL表的PHP PDO计数和总和值

Well, I'm pretty sure this is just a novice question, so please forgive me for that, but I feel like I'm losing my mind.

I have a simple MySQL rating table and I need to count rows and to sum rates values (int) with PHP PDO

$sql = "SELECT rate FROM rating_table";
$query = $db->query($sql);            

$rate_times = count($query->fetchAll()); // it works! 
echo '<p>'.$rate_times.'</p>';

$sum_rates = array_sum($query->fetchAll()); // it doesn't work!
echo '<p>'.$sum_rates.'</p>';

Thank you in advance for any suggestion

If I understand you right, all you have to do is to modify your sql request, this will return a single row

sql = "SELECT sum(rate) as rate_sum, count(*) as record_count FROM rating_table";
$query = $db->query($sql);            
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row) {
  $sum = $row['rate_sum'];
  $count = $row['record_count'];
}