尝试根据我从上一页的i按钮获取的ID从数据库中获取数据时出错。当我回显id时它只给了我正确的id,但在完整的查询中它给了我这个错误:
致命错误:带有消息的未捕获异常'PDOException' 'SQLSTATE [HY093]:参数号无效:没有参数绑定' 在
我在这上面找到了一些答案,但没有一个能解决我遇到的问题。
<?php
$fetch_article_info = $db->query("select * from news_article where id = :id");
$fetch_article_info->execute(array(':id' => $_GET['id']));
$list = $fetch_article_info->fetchAll(PDO::FETCH_ASSOC);
?>
以下是我认为可能有用的db.class中的一些公共功能
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
// database handler
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch (PDOException $e) {
$this->error = $e->getMessage();
}
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
答案 0 :(得分:1)
您没有进入执行功能。见下文。
$db->execute(array(':id' => $_GET['id']));
$list = $db->resultset(PDO::FETCH_ASSOC);
print_r($list);
// db.php file
public function execute($params){
return $this->stmt->execute($params);
}
public function resultset(){
// $this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}