Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get connection resource from PDO object?

Tags:

php

pdo

odbc

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?

like image 205
alkaponey Avatar asked Dec 26 '12 14:12

alkaponey


People also ask

Does PDO close connection?

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.

What is a PDO connection?

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.

What is dsn in PHP?

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.

What is PDO in SQL?

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.


1 Answers

I would think you have two choices:

  1. 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.

  2. dig out the source to the PDO object from the PHP source and build what you need from there.

like image 87
ethrbunny Avatar answered Oct 02 '22 21:10

ethrbunny