Check if a specific mysql connection exists during a php script?

I have a class for each database table object. Each class takes care of the connection / query / data format (specific database logic), since I prefer to assign a connection to each class for better modularity. In addition, I use certain joins for some tables and queries.

My question is: how can I check if a connection exists, so it won’t start another?

Basically, I want to check if a connection has already been made with the same username / password / database. Or is it not necessary because mySql does not start a new connection for the same user? If so, please explain.

After further research, it turned out that this is impossible ... for this you will need to get the stream identifier and use conn with the stream identifier, and it will act as a stable contact; Or you would need to keep track of all the thread IDs used during the script, and this is useless.

There is still no easy way to do this ... a persistent connection may be a choice, but then you need to take care of the connection by clearing and sux again :)

PS: In the end, I made a tracking system for connections during my launch of the application (the guys who said to save them in a globally accessible object, we are right). Stored conn object and conn params in 2 arrays in a single-element class object, it is checked whether there are params allready in the params array, if you do not create a new conn, if so, get the conn object from the key array, the same with the key where params were found ... I already had an architecture made for this, but didn’t want to use this class ... not the logic that I started with :), but also wanted to do it in a library element, which is why I was looking for a simple and abstract solution but there is only a specific solution :)

+4
source share
3 answers
  • Creating a new join for each class is not a good idea. It may be modular for you, but your mysql server will soon bloat with an error too may connections .

I suggest using singleton pattern and some OO.

 class Singleton{ private static $instance=null; public function connection(){ if(self::$instance==null){ self::$instance = mysql_connect(); // define it in your way, } return self::$connection; } } class TableA extends Singleton{ function find($id){ $query="select * from `A` where `id`='$id'"; mysql_query($query, $this->connection()); ... // other codes } } 
0
source

I suggest you read this - I think this will help you http://www.php.net/manual/en/features.persistent-connections.php

0
source

If you must have a different connection for each class, you can use the property of the static class to store the connection and use the singleton template to get it.

 class SomeTable { // Connection resource as a static property // Since it is static, it will be shared by all instances of class SomeTable public static $db = FALSE; // Static method to retrieve the connection // or create it if it doesn't exist public static function get_conn() { if (self::$db) { // Just return the connection if it already exists return self::$db; } else { // If it doesn't already exist, create it and return it self::$db = mysql_connect(...); return self::$db; } } // In other methods, use self::get_conn() public function someQuery() { $sql = "SELECT col FROM tbl"; // Call the connection singleton explicitly // as the second param to mysql_query() $result = mysql_query($sql, self::get_conn()); } } 
0
source

All Articles