This question already has an answer here:
I'm starting using PDO
and MySQL
instead of mysqli
. So, i try to define this simple function:
public function getIntro($table) {
$stmt = $this->db->prepare("SELECT * FROM $table ORDER BY rand() LIMIT ?");
$stmt->execute(array(6));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
But, in doing so, the function return an empty array. Considering that the connection with the db work correctly, where is the error?
Thanks
</div>
Try this:
public function getIntro($table) {
$stmt = $this->db->prepare("SELECT * FROM $table ORDER BY rand() LIMIT ?");
$stmt->bindValue(1, 6);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
or this:
public function getIntro($table) {
$stmt = $this->db->prepare("SELECT * FROM $table ORDER BY rand() LIMIT :limit");
$stmt->execute(array(':limit' => 6));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}