请考虑以下事项:
$PDOStatement = $PDO->prepare($query);
是否可以从$PDO
实例中获取$PDOStatement
实例?
答案 0 :(得分:2)
目前,这是不可能的。即使每个PDOStatement实例都存储了用于创建它的数据库句柄(quoting lxr for PHP 5.6):
/* represents a prepared statement */
543 struct _pdo_stmt_t {
544 /* these items must appear in this order at the beginning of the
545 struct so that this can be cast as a zend_object. we need this
546 to allow the extending class to escape all the custom handlers
547 that PDO declares.
548 */
549 zend_object std;
550
...
572 /* we want to keep the dbh alive while we live, so we own a reference */
573 zval database_object_handle;
574 pdo_dbh_t *dbh;
......它没有通过公共方法曝光。
值得注意的是,pdo_dbh_t
个实例可能(至少看起来如此)存储对pdo_stmt_t
(link)的引用:
427 /* represents a connection to a database */
428 struct _pdo_dbh_t {
...
501 /* when calling PDO::query(), we need to keep the error
502 * context from the statement around until we next clear it.
503 * This will allow us to report the correct error message
504 * when PDO::query() fails */
505 pdo_stmt_t *query_stmt;