PHP: How to get MySQL link id for an already open connection?

When you open a MySQL connection with mysql_connect (), it returns the link identifier. But what if you want to get this link id again later in the script? (for example: a plug-in that needs to open a new database connection and still access the old one.)

I am looking for a way to return the link identifier to the last connection opened by mysql_connect (). Is there a function that does this?

+4
source share
6 answers

Thanks everyone for the answers.

I ended up opening my own connection, and when my plug-in was completed, the last line opens the original connection again so that the rest of the main script can use the database that it expects. This is careless, I know, but there seems to be no better option in this case.: /

Update :: I presented this as a function request on php.net. Link: http://bugs.php.net/bug.php?id=49400

+1
source

Although Anti Veeranna’s explanation is correct, is a very easy way to do what you want. If you want to read the documentation for mysql_connect () , you will find out that this function has a 4th parameter, "new_link", which is false by default. If you call mysql_connect () again with the same parameters that you are likely to have, it will not create a new connection, but instead will return the resource identifier of the old one.

Any time;)

+7
source

No no. You must write the link identifiers yourself. You can define the link identifier as a property of the class so that you can easily access it from your methods and not have to worry about passing it as a variable over and over again.

+2
source

You must save your handle in var.

$link = mysql_connect($host, $login, $pass); 

And you can reuse $ link along your script before a new HTTP request.

0
source

You either need to save the link identifier yourself, as indicated in other answers, or simply omit it, mysql_ * functions can reuse an existing descriptor

mysql_query help explains this as follows:

 resource mysql_query ( string $query [, resource $link_identifier ] ) link_identifier The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. 

I would not use this reuse behavior, although if you are not completely sure that your application (or any plugins, etc.) does not open any new connections to other databases.

0
source

Use mysql_pconnect , which will do what you want.

Firstly, when connecting, the function will first try to find a (permanent) link that is already open with the same host, username and password. If it is found, the identifier for it will be returned instead of opening a new connection.

You can also use a static or singleton class to manage the connection pool for you.

 <?php class dbConnectionPooler { static var $connections = array(); private static function createConnection($host, $username, $password, $db) { $connection = mysql_pconnect($host, $username, $password); if (FALSE !== $connection) { $result = mysql_select_db($db, $connection); if (FALSE !== $result) { self::$connections[self::getConnectionHash($host, $username, $password, $db)] = $connection; return $connection; } } } private static function getConnectionHash($host, $username, $password, $db) { return md5($host. $username. $password. $db); // use your favourite hashing function here } public static function getConnection($host, $username, $password, $db) { $connectionHash = self::getConnectionHash($host, $username, $password, $db); if (array_key_exists($connectionHash, self::$connections)) { return self::$connections[$connectionHash]; } else { return self::createConnection($host, $username, $password, $db); } return false; } } $connection = dbConnectionPooler::getConnection("dbhost", "dbuser", "dbpassword", "mydb"); ?> 
-1
source

All Articles