PHP MYSQL PDO SUM of columns

前端 未结 3 949
生来不讨喜
生来不讨喜 2021-01-13 03:48

I\'m new to php and I\'ve searched for the past hour and read all the documentation I could find and nothing is helping. I have a table that has a bunch of rows of data. I\'

3条回答
  •  無奈伤痛
    2021-01-13 04:18

    You're only returning one row in this instance. Modify your summed column to have an alias:

    SELECT SUM(distance) AS totDistance FROM trip_logs ....
    

    Now you can can fetch the row -

    $row = $res1->fetch(PDO::FETCH_ASSOC);
    echo $row['totDistance'];
    

    No need to loop.

提交回复
热议问题