How does PHP know the last database connection when mysql_select_db () or mysql_query () is used without an optional database parameter?

Consider the following code:

<?php $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('database', $conn); ?> 

This works as expected, but how does PHP know which database connection to use when calling mysql_select_db() in the following example?

 <?php mysql_connect('localhost', 'username', 'password'); mysql_select_db('database'); ?> 

The PHP documentation states that "If the link identifier is not specified, it assumes the last link opened by mysql_connect ()." ( PHP: mysql_select_db () )

Where is the last connection stored or retrieved from?

+7
php
source share
2 answers

I assume that the link to the last open connection is stored somewhere in memory to make things easier (since we usually often use only one connection).


Quick search for ext/mysql sources:
(All line numbers are in php_mysql.c - the source version is a random snapshot of PHP 5.3.2-dev from a couple of weeks ago, so they could have changed a bit)

  • The user-space function called mysql_connect seems to correspond to a level C function called php_mysql_do_connect (line 922)
  • The php_mysql_do_connect function calls php_mysql_set_default_link (line 832)
    • To keep the last open connection
  • There is also a php_mysql_get_default_link function (line 908)
  • This php_mysql_get_default_link function php_mysql_get_default_link called by mysql_select_db when there is no link passed to it (line 992)


And php_mysql_set_default_link calls this to save default_link :

 MySG(default_link) = id; 

This MySG is a macro defined this way (in php_mysql_structs.h ):

 #ifdef ZTS # define MySG(v) TSRMG(mysql_globals_id, zend_mysql_globals *, v) #else # define MySG(v) (mysql_globals.v) #endif 

I really like the global variable; -)


If you want, you can take a look at the sources yourself: ext/mysql/php_mysql.c and ext/mysql/php_mysql_structs.h .

As I said, this probably changed a bit from the version in which I checked - which means that line numbers may not match exactly; but function names are easy to understand, so you should be able to track what causes what and where :-)

+11
source share

I want this class to help you, I already wrote it ............ sorry that he needs testing

 class connectionManager(){ protected $array = array( "connection1" => "host3-username-password-database1" , "connection2" => "host2-username-password-database2" , "connection3" => "host1-username-password-database3" , ) protected $history = array(); public function __construct($connectionID = connection1){ $this->savelastConnecton($connectionID); /*do you magic here to change the string above to desired format sorry but i gatta go btw i liked your question */ mysql_connect($host , $username , $password); mysql_select_db($database); return true ; } publich function getLastConnection( ){ return end($this->history[$lastnumber]) ; } private function saveLastConnection($connectionID){} $this->history[] = $connectionID ; return true ; } 
0
source share

All Articles