View Single Post
Old 12-26-07, 11:08 PM   #5
Ngemu Mod
Join Date: Jul 2003
Location: Fresno, CA
Posts: 1,886
Default Re: PHP/Mysql Multiple Questions

Originally Posted by t3hl33td4rg0n
Could you do something like this?


class db {
	public $db1_srvr		= '';
	public $db1_port		= 3306;
	public $db1_name		= 'user';
	public $db1_pswd		= 'password';
	public $db1_db			= 'foo';

	public $db2_srvr		= '';
	public $db2_port		= 3306;
	public $db2_name		= 'user';
	public $db2_pswd		= 'password';
	public $db2_db			= 'foo';
	public $cdb1;
	public $cdb2;
	public $qr1;
	public $qr2;
	function db_conn ($server) {
		switch ($server) {
			case 1:
				$this->cdb1 = mysqli_connect($this->db1_srvr, $this->db1_name, $this->db1_pswd, $this->db1_db) or die(mysql_error());
			case 2:
				$this->cdb2 = mysqli_connect($this->db2_srvr, $this->db2_name, $this->db2_pswd, $this->db2_db) or die(mysql_error());
		return true;
	function GenericQuery ($c, $table) {
		$s = 'SELECT * FROM `'.$table.'` SORT BY `uid`';
		if ($c == 1) {
			$q = mysqli_query($cdb1, $s);
			$r = mysqli_fetch_array($q, MYSQL_ASSOC);
			$this->qr1 = $r;
		} elseif ($c == 2) {
			$q = mysqli_query($cdb2, $s);
			$r = mysqli_fetch_array($q, MYSQL_ASSOC);
			$this->qr2 = $r;
		} else {
			die("Invalid Server ID spec");
$dbX = new db();

That's similar to what I ended up doing. It took a bit more planning and re-arranging of the various tables, but the 2 databases in question are partitioned and working like I wanted. Of course I also needed to plan the queries to take this into account.

I'm using php5 objects and global defines to make the connection to both databases. The base parent object contains the connect/select_db functions which gets inherited by all other objects which need to connect to the 2 databases to make queries. Pretty much all queries are done in sub classes. I actually ended up with more simpler queries than less complex large queries, but this seems to work to my advantage.
[i7 2600k @4.4ghertz][2x4 GB DDR3 1600][EVGA GTX570 1.280GB SC][EVGA GTX460 physx][Asrock Extreme7 Gen3 Z68][2xSeagate 160 Gb SATA HD raid0][Seagate 250 GB SATA2 HD][Sony Bravia 40' 1080p LCD HDTV][NEC 3520a DVD+-DLw][Windows 7 Ultimate x64][Rosewill 1000w]

Last edited by fivefeet8; 12-31-07 at 12:03 PM.
fivefeet8 is offline   Reply With Quote