$query = "SELECT sum(load_d) FROM `aser` WHERE Y=3"; $stmt = $this->pdo->query($query); while ($row = $stn->fetch(PDO::FETCH_ASSOC)){ print "-----------------\n"; print $row['load_d']; } 

Tell me why the return of the amount of the column via PDO does not work? sql request is valid, checked through phpmyadmin.

  • And why did you decide that column X would be there? she's probably not there - KoVadim
  • @KoVadim why not? I know what it is and in phpmyadmin this request works and gives the result - makson
  • php admin shows that there is a column in the result set with the name X? - KoVadim

2 answers 2

PDO is somewhat smarter than other obsolete APIs, and does not require any special query tricks for getting the amount.

 $query = "SELECT sum(load_d) FROM `aser` WHERE Y=3"; $sum = $this->pdo->query($query)->fetchColumn(); echo $sum; 

Separately, I want to note that using a loop to get one line does not make sense.

    The request is valid, the column name is invalid.

     $query = "SELECT sum(X) as sum_value FROM `table` WHERE Y=3"; $stmt = $this->pdo->query($query); while ($row = $stn->fetch(PDO::FETCH_ASSOC)){ print "-----------------\n"; print $row['sum_value'];