2

I'm trying to get my LIMIT to accept the variable $limit, but it doesn't like my binding. Is this because the other $params are an array (string) as opposed to an integer? Is there at way to specify $limit as an integer?

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;
        }

    }
jessicaldale
  • 125
  • 1
  • 2
  • 11
  • This has been addressed in [another question](http://stackoverflow.com/questions/2269840/php-pdo-bindvalue-in-limit) where the solution is to coach PDO a bit better. You may need to bind a specific variable instead of using `execute` straight-up. Hopefully someone can explain how this applies to your situation as a more formal answer. – tadman Aug 28 '12 at 23:18
  • after reviewing the question several time I believe the error is in my syntax as I'm using shorthand to bind the variables. $params[':limit'] = $limit; – jessicaldale Aug 29 '12 at 17:01
  • for some reason this variable will not bind to the placeholder. any thoughts? – jessicaldale Aug 29 '12 at 17:02
  • I don't think you're supposed to put `:limit` as the key, but `limit` instead. The `:` only appears in the SQL statement itself to indicate it's a placeholder. – tadman Aug 29 '12 at 18:58

0 Answers0