首页 > 解决方案 > php无法识别其他方法中的pdo对象

问题描述

当我想$this->stmt->bindValue()在绑定值 php 风暴中使用时会出现错误:

在 PDO 中找不到方法绑定值

当我运行代码时,我看到了这个错误:

“PDOStatement::execute(): SQLSTATE[HY093]: 参数号无效:参数未在第 54 行的 C:\xampp\htdocs\oop\app\Model\DB.php 中定义

<?php  namespace App\Model;


class DB
{

    /**
     * @var PDO   <--- need by PhpStorm to find Methods of PDO
     */

    protected  $table ;
    /**
     * @var PDO   <--- need by PhpStorm to find Methods of PDO
     */
    protected $stmt ;
    protected $bind=[] ;
    protected $fetchMode = \PDO::FETCH_OBJ ;

    public function __construct()
    {
        $config= require_once (__DIR__.'/../config.php') ;


        try{
            $this->pdo = new \PDO("mysql:host=127.0.0.1;dbname={$config['db']['database']}",$config['db']['username']
,$config['db']['password']) ;

        } catch (\Exception $e)
        {
            die($e->getMessage());
        }

    } // end of constructor

    public  function select()
    {

        $stmt = $this->pdo->prepare("SELECT * FROM  {$this->table}") ;
        $stmt->execute();
      return  $stmt->fetchAll() ;

    } // end of select method

    public function create($data)
    {
        $field = join(',',array_keys($data)) ;
        $param = ':'. join(', :' , array_keys($data)) ;

        $this->stmt =  $this->pdo->prepare("INSERT INTO $this->table ($field) VALUES ($param)") ;
        $this->bind = $data ;

        $this->bindValue() ;

        return $this->stmt->execute();

    }

    public function bindValue()
    {

        var_dump($this->stmt);
        foreach ($this->bind as $key => $value){
            $this->stmt->bindValue(":$key " , $value) ; //error
        }

    }


} // end of DB class

标签: phppdobinding

解决方案


method bindvalue not found in PDO

This is correct. The PDO class represents the connection to the database, not the individual query/statement. For that there is the PDOStatement class.


推荐阅读