I need to get the connection resource from already existing PDO object. For example:
...
$oPDO = new PDO($sOdbcDsn);
$rOdbcConnection = $oPDO -> getConnection();
odbc_prepare($rOdbcConnection, $sQuery);
...
Also vice versa, I'd like to pass existing connection to PDO constructor. I want to have the ability to work with PDO objects and direct connections separately.
Is there any way to extract connection from PDO?
The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted—you do this by assigning null to the variable that holds the object.
To standardize and streamline development practices, PHP introduced PHP Data Objects (PDO) in PHP 5.1. These objects are used to setup PDO database connections. PDO is a database access layer which provides a fast and consistent interface for accessing and managing databases in PHP applications.
dsn. The Data Source Name, or DSN, contains the information required to connect to the database. In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax. Further information is available from the PDO driver-specific documentation.
PDO (PHP Data Objects) is an abstraction layer for your database queries and is an awesome alternative to MySQLi, as it supports 12 different database drivers. This is an immense benefit for people and companies that need it. However, keep in mind that MySQL is by far the most popular database.
I would think you have two choices:
wrap your PDO connection ( and methods ) in a class that you can pass around like you want. Define a copy constructor that will reuse the connection handle.
dig out the source to the PDO object from the PHP source and build what you need from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With