目前我将准备好的语句保存到一个私有变量中,因为我忽略了它们在深度中的实际工作方式,以防万一。
所以这个问题真的很简单,如果我迭代相同的$PDO->prepare()
,它会再次准备相同的查询吗?
foreach( $arr as $docid ) {
if( $this->dbLink === null ) { // PDO resource, saved in the object.
throw new Exception( 'Must first connect to DB' );
}
if( $this->queryCheckAccess === null ) {
$query = 'SELECT * from something where id = :id';
$this->queryCheckAccess = $this->dbLink->prepare($query);
}
else {
$result = $this->queryCheckAccess->execute(array(':id'=>$docid));
}
}
有关系吗?或者数据库引擎/PHP 足够聪明,知道它是同一个准备好的语句?
非常感谢。
- - - - - - - - - 编辑 - - - - - - -
我想我被误解了。
我要问的是如果我这样做会发生什么:
$query = 'SELECT * from something where id = :id';
$this->queryCheckAccess = $this->dbLink->prepare($query);
$query = 'SELECT * from something where id = :id';
$this->queryCheckAccess = $this->dbLink->prepare($query);
$query = 'SELECT * from something where id = :id';
$this->queryCheckAccess = $this->dbLink->prepare($query);
$query = 'SELECT * from something where id = :id';
$this->queryCheckAccess = $this->dbLink->prepare($query);
如果我这样做会发生什么:
if( $this->queryCheckAccess === null ) {
$query = 'SELECT * from something where id = :id';
$this->queryCheckAccess = $this->dbLink->prepare($query);
}
引擎会在第一个示例中准备 4 次查询吗?或者会注意到它是同一个查询并且只是“跳转”那个?