douke1905 2015-08-31 14:50
浏览 48
已采纳

获取一个帐户中使用的GB数量时出错

When I upload a video in my website, its size is saved in the database. And then, I use these functions to get the amount of gygabites left in the account used.

    public static function Capacidad($user){
        $sql = "SELECT tipocuenta.id, tipocuenta.capacidad FROM tipocuenta, canal WHERE tipocuenta.id = canal.id_tipocuenta AND canal.id = '$user'";
        $resultado = self::Conexion($sql);

        if(isset($resultado)){ $datos = $resultado->fetch(); }

        $capacidad = $datos["capacidad"];

        return $capacidad;
    }

    public static function CapacidadUsada($user){
        $sql = "SELECT sum(tamaño) FROM video WHERE usuario_id = '$user'";
        $resultado = self::Conexion($sql);
        return $resultado;
    }

    public static function CapacidadUsadaGB(){
        $capacidad_total = Database::Capacidad($_SESSION["usuario"]);
        $capacidad_usada = Database::CapacidadUsada($_SESSION["usuario"]);

        $capacidad_usadaGB = number_format($capacidad_usada / 1073741824, 2)."GB";

        return $capacidad_usadaGB;
    }

I get the amount of GB that an user can use and the amount of GB that the user have used uploading videos. But it doesnt work... It prints me this error:

Notice: Object of class PDOStatement could not be converted to int
  • 写回答

1条回答 默认 最新

  • 普通网友 2015-08-31 15:01
    关注

    The function CapacidadUsada() returns a result set, not a number. That's why the line number_format($capacidad_usada / 1073741824, 2) would throw an error.

    Update the CapacidadUsada() function to return a number, just like Capacidad():

    public static function CapacidadUsada($user){
        $sql = "SELECT sum(tamaño) AS usada FROM video WHERE usuario_id = '$user'";
        $resultado = self::Conexion($sql);
    
        if(empty($resultado)) 
            throw new Exception('Error retrieving usage.');
        $datos = $resultado->fetch();
    
        return $datos['usada'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏