[php] probleempje met een klasse (OO)

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
PHP:
1
2
3
4
5
6
7
8
9
10
/* create Mysqldb object that connects to this applications database */
$db = new Mysqldb();
$db->connect("localhost", "xxx", "xxx" );
$db->selectdb("develop");
    
/* create Mysqldb object that connects to the EAS database */
/* this is the financial application */
$eas = new Mysqldb();
$eas->connect("localhost", "xxx", "xxx" );
$eas->selectdb("eas");


class:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Mysqldb {
    
    var $db_link;
    var $result;
    var $result_set;
    var $numRows;
    var $affectedRows;
    var $lastInsertId;
    
    //default constructor
    function Mysqldb(){
    }
    
    //connect function
    function connect( $server, $username, $password ){
        $this->db_link = mysql_connect( $server, $username, $password ) or die ( "<p>Not connected" );
    }
    
    //select database
    function selectdb( $dbname ){
        $this->db = mysql_select_db( $dbname, $this->db_link ) or die ("<p>No database selected");
    }
}


Ik gebruik deze class al een tijdje op 1 database en dat werkte altijd prima.
Nu wilde ik de $eas erbij gaan gebruiken, maar die overruled de $db op een oftandere manier?
Is het niet zo dat $db en $eas hun eigen variabelen ($db_link ed) hebben?

Acties:
  • 0 Henk 'm!

Verwijderd

Uit de php-manual:
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. The client_flags parameter can be a combination of the constants MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE.

Note: The new_link parameter became available in PHP 4.2.0
Dus als zowel 'develop' als 'eas' op dezelfde server staan (zoals uit je code blijkt), zal de tweede aanroep geen nieuwe link retourneren, tenzij je de new_link parameter op true zet.

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
thnx sir5!
true erachter fixed the problem! :)