Database Class Help

From: Date: Thu, 01 Dec 2005 06:42:14 +0000
Subject: Database Class Help
Groups: php.general 
Request: Send a blank email to php-general+get-226682@lists.php.net to get a copy of this message
Hi, We have a database class that we want to use to connect to multiple databases at the same time using different variables. Here is part of the db class: class db { var $dbh = ""; var $q = ""; function db($db_host, $db_user, $db_pass, $db_name) { $this->dbh = @mysql_pconnect($db_host, $db_user, $db_pass); $this->select_db($db_name); } function select_db($db) { if (!@mysql_select_db($db, $this->dbh)) { die("<em>Cannot select database because:</em></p>\n<p><code>" . mysql_error() . "</code>"); } } function num_rows($_q) { if (!$this->q = mysql_query($_q, $this->dbh)) { die("Invalid query \"<code>" . $_q . "</code>\"</p>\n<p><code>" . mysql_error() . "</code>"); } else { return mysql_num_rows($this->q); } @mysql_free_result($this->q); } } In my php file I say something like <?php Include ( 'db.php' ); $db_1 = new db('server','username','password','db1'); $db_2 = new db('server','username','password','db2'); Echo $db_1->num_rows('select * from table1'); Echo $db_2->num_rows('select * from table2'); ?> Now that happens is when I try and do the query for db1, I get told that db2.table1 doesn't exist. I can swop anything around and it doesn't seem to work for one of the classes. Does anyone have any idea as to why this is happening? Thanks a lot, Ian

« previous php.general (#226682) next »