PHP MYSQL PDO SUM of columns

前端 未结 3 955
生来不讨喜
生来不讨喜 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:03

    Try this if you are using a Class :

        class Sample_class{
    
            private $db;
    
            public function __construct($database) {
                $this->db = $database;
            }   
    
            public function GetDistant($user_id,$status) {
    
                        $query = $this->db->prepare("SELECT sum(distance) FROM trip_logs WHERE user_id =? AND status =?");
    
                        $query->bindValue(1, $user_id);
                        $query->bindValue(2, $status);
    
                        try{ $query->execute();     
    
                         $rows =  $query->fetch();
                         return $rows[0];
    
                  } catch (PDOException $e){die($e->getMessage());}  
                } 
        }
    
    $dist = new Sample_class($db);
    
    $user_id = 10;
    $status = 2;
    
    echo $dist->GetDistant($user_id,$status);
    

提交回复
热议问题