Emanuil Rusev
Emanuil Rusev

Reputation: 35245

Get PDO from PDOStatement

Consider the following:

$PDOStatement = $PDO->prepare($query);

Is it possible to get the $PDO instance from the $PDOStatement instance?

Upvotes: 1

Views: 198

Answers (1)

raina77ow
raina77ow

Reputation: 106375

Currently, it's not possible. Even though each instance of PDOStatement stores a DB handle used to create it (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;

... it's not exposed via public methods.


It might be worth of a note that pdo_dbh_t instances in turn may (at least it seems so) store references to 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;

Upvotes: 2

Related Questions