Nasıl PHP () bağlı veritabanı parametre olmadan kullanıldığında mysql_select_db () veya mysql_query son veritabanı bağlantısı ne olduğunu biliyor mu?

2 Cevap

Aşağıdaki kodu göz önünde bulundurun:

<?php

$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $conn);

?>

Bu beklendiği gibi çalışır, ancak aşağıdaki örnekte mysql_select_db() çağırırken nasıl PHP ne veritabanı bağlantısı kullanmayı biliyor mu?

<?php

mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');

?>

PHP belgelerine devletler ") link tanımlayıcı belirtilmemişse, son bağlantı mysql_connect (tarafından açılan kabul edilir." (PHP: mysql_select_db())

Nerede son bağlantı saklanan veya alınır?

2 Cevap

Ben son açılan bağlantı için bir link (as we generally often use only one connection) işleri kolaylaştırmak için, bir yere bellekte tutulur varsayalım.


Quickly going through the sources of ext/mysql :
(All line numbers are in php_mysql.c -- the version of the sources is a random snapshot of PHP 5.3.2-dev from a couple of weeks ago ; so, they might have changed a bit)

  • mysql_connect seems to correspond to the C-level function called php_mysql_do_connect (line 922) adlandırılan alan kullanıcı işlev
  • The php_mysql_do_connect function calls php_mysql_set_default_link (line 832)
    • Son açılan bağlantıyı saklamak için
  • php_mysql_get_default_link (line 908) adlı bir fonksiyonu da vardır
  • Bu php_mysql_get_default_link function is called by mysql_select_db , when there is no link passed to it (line 992)


And php_mysql_set_default_link is calling this to store the default_link :

MySG(default_link) = id; 

İşte MySG bu gibi tanımlanmış bir makro olmanın (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 

Hemen hemen benim için bir global değişken gibi görünüyor ;-)


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 has probably been modified a bit since the version in which I checked -- which means the line numbers might not match exactly ; but the functions names are easy anough to understand, so you should be able to track down what calls what and where :-)

Ben bu sınıf ben zaten üzgünüm test ihtiyacı ............ bunu yazmıştım, size yardımcı olacaktır diliyorum

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 ;    

}