我开始使用PDO
和MySQL
代替mysqli
。
所以,我尝试定义这个简单的函数:
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);
}
但是,这样做,函数返回一个空数组。考虑到与db的连接正常工作,错误在哪里?
由于
答案 0 :(得分:0)
试试这个:
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);
}
或者这个:
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);
}