Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a PDO handle

Tags:

php

sqlite

pdo

I'm using PDO to access two SQLite 3 databases in PHP. I want to switch the database files during a query by renaming them but I can't do that while the files are open as it gives an error that the file is being used by another process. I've tried turning off persistent connections and setting the handles to null but neither work.

Is there really no way to close a PDO handle and release the lock on the database file?

like image 489
Tamlyn Avatar asked Aug 15 '11 16:08

Tamlyn


People also ask

Is it necessary to close PDO connection?

So the answer is no, you don't need to do anything unless you need to explicitly close the connection during the script execution for whatever reason, in which case just set your PDO object to null.

How do I set up PDO?

Pdo ( Portable Data Object ) needs to be installed if it is not done before. For windows platform go to control panel > Add remove program ( or Programs and features ) > Select your PHP installation and click Change. If you are installing PHP fresh then in the setup wizard you can select PDO from Extensions link.

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.


2 Answers

I believe unset($var) does that, I use it on my pdo sqlite project and it works like I want it to :)

like image 110
gnur Avatar answered Sep 26 '22 03:09

gnur


Set all references to the handle to null (or to anything except the PDO object, really) and the runtime will destruct the object, which will close the connection.

$db = new PDO('...');
// Do some stuff

$db = null;
// Assuming this was the last reference to that PDO
// object, the runtime will destroy the object and
// its connection.
like image 21
cdhowie Avatar answered Sep 23 '22 03:09

cdhowie