使用PDO和MySql而不是mysqli

时间:2016-04-28 21:30:20

标签: php mysql mysqli pdo

我开始使用PDOMySQL代替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的连接正常工作,错误在哪里?

由于

1 个答案:

答案 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);
        }