我得到了
警告:PDO :: prepare()期望参数2为数组,整数 在第29行的/var/www/html/mvc_patern/libs/Model.php中给出
Main Model.php
<?php
class Model{
public $table;
public $id;
protected $_db;
public function __construct($username = "mvcpatern", $password = "mvcpatern", $host = "localhost", $dbname = "mvcpatern", $options = []){
// $this->pdo = TRUE;
try {
$this->_db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
// get row
public function getRow($id){
try {
$stmt = $this->_db->prepare(
"SELECT *
FROM `{$this->table}`
WHERE id={$id})",
PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetch();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
}
?>
在subModel
中 <?php
class Article_Model extends Model {
public $table = 'articles';
public $message;
public function __construct() {
parent::__construct();
}
}
?>
最终调用方法
<?php
$message = new Article_Model();
$message->getRow(1);
?>
我的方法getRow有什么问题?为什么它要求参数数组,如果我定义了&#34; table&#34; &安培; &#34; ID&#34;