繁体   English   中英

消息“ SQLSTATE [HY093]”的未捕获异常“ PDOException”:从数据库中获取信息时

[英]Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: while getting information out of the database

尝试根据上一页中从i按钮获取的ID从数据库中获取数据时遇到错误。 当我仅回显id时,它给了我正确的ID,但在完整查询中却给了我这个错误:

致命错误:消息中出现“ SQLSTATE [HY093]:未捕获的异常” PDOException”:无效的参数号:未绑定任何参数”

我对此找到了一些答案,但没有一个解决了我遇到的问题。

<?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);
    }

您没有传递给您的execute函数。 见下文。

$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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM