...

View Full Version : Resolved SELECT Statement using PDO returns empty array only if I use bindParam()



SirDarren
12-18-2010, 12:14 AM
The following code returns the perfect array I'm looking for:


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:


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum