2

我试图让我的 LIMIT 接受变量 $limit,但它不喜欢我的绑定。这是因为其他 $params 是数组(字符串)而不是整数吗?有没有办法将 $limit 指定为整数?

public function getPhotos($limit, $status, $type, $session, $name, $inc_suite = NULL) {
        $params = array();
        $sql = 'SELECT * FROM images WHERE 1=1';

        /**
         * Filter by status
         */
        if (!is_null($status) and $status != 'all') {
            $sql .=' AND status = :status';
            $params[':status']= $status;
        }

        /**
         * Filter by type
         */
        if ($type !="null") {
            $sql .=' AND type = :type';
            $params[':type'] = $type;
        }

        /**
         * Filter by session
         */
        if ($session !="null") {
            $sql .=' AND session = :session';
            $params[':session'] = $session;
        }

        /**
         * Filter by handle
         */
        if ($name !="") {
            $sql .=' AND handle = :name';
            $params[':name'] = $name;
        }

        /**
         * Block Suite photos
         */
        if($inc_suite != 1) {
            $sql .= " AND type != 'suite'";
        }

        /**
         * Set order and limit the number of photos
         */
        $sql .=' ORDER BY created DESC LIMIT :limit';
        $params[':limit'] = (int)$limit;


        try {
            /**
             * Prepare the query
             */
            $stmt = $this->pdo->prepare($sql);

            /**
             * Fire the lasers
             */
            $stmt->execute($params);

            /**
             * Grab results
             */
            $result = $stmt->fetchAll();
            return $result;


        } catch(PDOException $e) {
            $errors['database'] = $e->getMessage();
            return FALSE;
        }

    }
4

0 回答 0