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 :-)
Pascal martin
source share