如何使用PDO获取列的总和?

时间:2014-01-25 12:22:12

标签: php mysql pdo sum

php说它无法转换为int:

注意:第76行的C:\ xampp \ htdocs \ elearning \ pages \ student \ upload.php中无法将类PDOStatement的对象转换为int:

              $userLoggedIn = $_SESSION['username'];

              $stmt = $conn->prepare(  "SELECT s.grade_level, s.section 
                                        FROM tblusers as u 
                                        JOIN tblstudents as s 
                                        ON u.username = s.username 
                                        WHERE u.username = :username" );
                                        $stmt->execute(array( ':username' => $userLoggedIn ));

              $file = $_FILES['file']['name'];
              $filesize = $_FILES['file']['size'];
              $filepath = "../../assets/uploads/" . $file;

              $filelimit = $conn->prepare("SELECT SUM(f.filesize)
                                           FROM tblfiles AS f 
                                           JOIN tblstudents AS s
                                           ON f.uploader = s.username
                                           WHERE f.uploader = ':username'");
              $filelimit->execute(array(':username'=>$userLoggedIn));

这是第76行:if($ filelimit> 1048576)

3 个答案:

答案 0 :(得分:0)

我猜您从sum(f.filesize)获取值时遇到问题。

尝试给它一个像这样的别名

  SELECT SUM(f.filesize) as filesize

然后通过此filesize

检索总和

答案 1 :(得分:0)

$filelimitPDOStatement个对象。要获取结果集,必须从语句中获取结果集。

MrVimes有完整的答案。

答案 2 :(得分:0)

$ filelimit是一个PDO对象。不是整数。您需要使用fetch()

从对象获取查询结果
$row = $filelimit->fetch();

$filelimitvalue = $row[0];

然后在第76行的if语句中......

if($filelimitvalue > 1048576)