drtyrell Posted February 2, 2011 Share Posted February 2, 2011 Okay, real simple issue, hopefully someone has a simple solution. I have a single web page that needs to connect to TWO different databases. I've written everything in OO, and I'm getting a crazy situation where vars seem to be bleeding into each other. The problem goes like follows: I declare two SEPARATE database instances like so: require_once '/blah/classes/db/class.DBCMS.php'; require_once '/blah/classes/db/class.DBC.php'; $db1 = new DBC(); $db2 = new DBCMS(); I then pass in the $db(n) variables to a separate class to be used to process data like so: $publisher = new htmlCreator1($db1); $publisher = new htmlCreator2($db2); My problem is thus: INSIDE the respective different classes, my references to the $db vars seem to be colliding between instances. So $this->db->connection from db2 is referencing db1. The objects are defined like so: class htmlCreator1 { var $db; function __construct($db) { $this->db = $db; } } Shouldn't the respective $this->db's maintain their own namespace without extra work? I'm getting errors saying that tables in db2 aren't in db1 because of this collision as per the internal db instance vars. I have experimented with renaming all respective vars to unique names and still get the error. I'm crazy aren't I? Any help would be greatly appreciated. Thx, Dr. Quote Link to comment https://forums.phpfreaks.com/topic/226412-oo-var-collision-am-i-going-crazy/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2011 Share Posted February 2, 2011 $publisher = new htmlCreator1($db1); $publisher = new htmlCreator2($db2); Based on the code you posted ^^^, you are overwriting the first instance in $publisher with the second instance. Quote Link to comment https://forums.phpfreaks.com/topic/226412-oo-var-collision-am-i-going-crazy/#findComment-1168640 Share on other sites More sharing options...
drtyrell Posted February 2, 2011 Author Share Posted February 2, 2011 Sorry, that was a typo in trying to put together psuedo code for the forum. They are going into unique vars. Quote Link to comment https://forums.phpfreaks.com/topic/226412-oo-var-collision-am-i-going-crazy/#findComment-1168645 Share on other sites More sharing options...
trq Posted February 2, 2011 Share Posted February 2, 2011 The problem you describe is not possible, so where going to need to see actual relevant code, not pseudo anything. Quote Link to comment https://forums.phpfreaks.com/topic/226412-oo-var-collision-am-i-going-crazy/#findComment-1168654 Share on other sites More sharing options...
drtyrell Posted February 2, 2011 Author Share Posted February 2, 2011 Alright you geniuses. Here's the solution found on stackoverlfow dot com: "The next parameter to mysql_connect is $new_link, can you try sending in a true there and see if it helps? $this->connection = mysql_connect($host, $username, $password, true);" With TRUE one gets unique connection objects. If you don't know, just say you don't know. Quote Link to comment https://forums.phpfreaks.com/topic/226412-oo-var-collision-am-i-going-crazy/#findComment-1168656 Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 You are nice. I like you. Quote Link to comment https://forums.phpfreaks.com/topic/226412-oo-var-collision-am-i-going-crazy/#findComment-1168660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.