php - 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
解决方案
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.
推荐阅读
- python - Python 最佳模块导入实践
- regex - 正则表达式提取包含特定字符串的两个单词之间的字符串
- bash - 删除空格以重组电子邮件地址
- python - 创建新列,其中来自两个不同数据框的两列相同
- c# - 在func委托c#中访问闭包变量
- java - 将所有对象放在 xml 文件中的一个位置或创建每个类的最佳方法是什么?
- ios - Xamarin Forms NavigationPage small part of previous page visible when navigating on iOS
- objective-c - 如何以编程方式在 MacOS 中隐藏全屏应用程序
- python - 正则表达式字符串字典python
- angular - 在 ngrx 存储中使用实体组合选择器