0

I'm tryng to connect a different database from a php function (assuming there's a current connection to another database). I'm using mysql_connect() with new_link parameter set to TRUE as you can see below. How is it possible the following code returns global_thread_id=16357138 local_thread_id=16357139 current_global=16357139 (meaning local connection overriden previous connection) despite the TRUE in mysql_connect()

Also in php settings, sql.safe_mode = OFF

// Class static method
static function Query($sql) {
       $global_thread_id = mysql_thread_id();
       if ($link = mysql_connect(FB_DB_HOST, FB_DB_USER, FB_DB_PASS, true)) 
       {
            $local_thread_id = mysql_thread_id($link);                          
            echo 'global_thread_id='.$global_thread_id.' local_thread_id='.$local_thread_id.' current_global='.mysql_thread_id();   
       }
}
Stephane
  • 4,978
  • 9
  • 51
  • 86

1 Answers1

1

mysql_thread_id() fetch the latest thread id, not a "global"

php manual for mysql_thread_id says:

Retrieves the current thread ID

// Class static method
static function Query($sql) {

       /* fetch latest thread id = global */
       $global_thread_id = mysql_thread_id();
       if ($link = mysql_connect(FB_DB_HOST, FB_DB_USER, FB_DB_PASS, true)) 
       {
            /* fetch thread id from $link */
            $local_thread_id = mysql_thread_id($link);

            /* echo 2 vars and the latest thread id = same as link */
            echo 'global_thread_id='.$global_thread_id.' local_thread_id='.$local_thread_id.' current_global='.mysql_thread_id();   
       }
}
Puggan Se
  • 5,738
  • 2
  • 22
  • 48
  • yes I know. What I mean by "global" is my script has a main connection used everywhere on the page thus the mysql_thread_id() without $link to fetch this main thread ID (hence I call it global connection) and for this second connection I'm using the $link resource to fetch what I call "local" thread id... – Stephane Aug 24 '12 at 07:08
  • now my issue is the thread ID returned by mysql_thread_id() equals $local_thread_id after the mysql_connect() call despite the 'new_link' parameter set to TRUE – Stephane Aug 24 '12 at 07:11
  • if you have more then one database connection, always use the $link varibale when talking with mysql, my usal approach is to have a database class, where i set the $link as a class variable. but if you going to fix all your mysql-calls, you may want to move from thous old depricated functions to PDO – Puggan Se Aug 24 '12 at 07:44