php无法以其他方法识别pdo对象

时间:2018-11-08 14:00:38

标签: php pdo binding

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

  

在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

1 个答案:

答案 0 :(得分:0)

  

在PDO中找不到方法绑定值

这是正确的。 PDO 类表示与数据库的连接,而不是单个查询/语句。为此,有一个 PDOStatement 类。