Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    SELECT Statement using PDO returns empty array only if I use bindParam()

    The following code returns the perfect array I'm looking for:
    PHP Code:
                public static function getPhotos($userId$perPage$offset)
                {
                        global 
    $database;
                        
    $sql "SELECT * FROM photo WHERE UserID = {$userId} LIMIT {$perPage} OFFSET {$offset}";
                        
    $stmt $database->connection->prepare($sql);
                       
                        
    $stmt->execute();
                       
                        
    $foundPhotos $stmt->fetchAll(PDO::FETCH_ASSOC);
                        return 
    $foundPhotos;
                } 

    The following code returns an empty array:
    PHP Code:
    public static function getPhotos($userId$perPage$offset)
                {
                        global 
    $database;
                        
    $sql "SELECT * FROM photo WHERE UserID = :userId LIMIT :perPage OFFSET :offset";
                        
    $stmt $database->connection->prepare($sql);
                       
                        
    $stmt->bindParam(':userId'$userId);
                        
    $stmt->bindParam(':perPage'$perPage);
                        
    $stmt->bindParam(':offset'$offset);
                          
                        
    $stmt->execute();
                           
                        
    $foundPhotos $stmt->fetchAll(PDO::FETCH_ASSOC);
                        return 
    $foundPhotos;
                } 

    I want to use the latter because of prepared statements, but I don't know why it returns an empty array when the first function will work just fine.

    Any help much appreciated!

    EDIT: I got it. I had to declare the type as PDO::PARAM_INT for each of the bindParam()s.
    Last edited by SirDarren; 12-18-2010 at 01:28 AM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •