为了调试目的,我希望能够克隆一个PDOStatement,或者在不从语句中删除行的情况下获取数据。
这里有一个例子来展示我想要做的事情:
public function execute($query)
{
// Prepare the statement
if (!($stmt = $this->prepare($query))) {
return false;
}
// Execute the statement
$stmt->execute($params);
// Print the query with a custom debug tool
if( $config->debug ) {
Debug::print($query);
Debug::print($stmt->fetchAll(PDO::FETCH_ASSOC));
}
// Return the PDOStatement
return $stmt;
}问题是fetchAll()完成了它的工作,删除了语句中的每个结果,函数返回了一个空数组。
我正在搜索一种方法来打印查询结果(调试目的)并返回初始语句(处理目的),,而无需查询我的数据库两次!
我试图复制我的声明,但没有成功:
if( $config->debug ) {
$_stmt = clone $stmt;
Debug::print($query);
Debug::print($_stmt->fetchAll(PDO::FETCH_ASSOC));
}知道吗?
发布于 2014-10-15 13:19:16
PDOStatement是不可复制的。
实现我想要的目标的唯一方法是重新执行该语句:
public function execute($query)
{
// Prepare the statement
if (!($stmt = $this->prepare($query))) {
return false;
}
// Execute the statement
$stmt->execute($params);
// Print the query with a custom debug tool
if( $config->debug ) {
Debug::print($query);
Debug::print($stmt->fetchAll(PDO::FETCH_ASSOC));
$stmt->execute($params);
}
// Return the PDOStatement
return $stmt;
}发布于 2017-08-02 15:02:02
与其重复查询,不如扩展PDOStatement并使PDO使用
$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [yourclass]::class);在那门课上,你可以使PDOStatement成为可寻的。缓存fetch/fetchAll/fetchColumn函数的结果。
https://stackoverflow.com/questions/25407537
复制相似问题