I have created these two files:
class myDbClass {
function dbConnect($db) {
$dbhost = 'myhost';
$dbuser = 'myuser';
$dbpass = 'mypassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($db, $conn);
$this->dbConnection = $conn;
}
function dbSelect($sql) {
// run the query
if ($result = mysql_query($sql, $this->dbConnection)) {
echo 'got a result';
} else {
echo 'error';
}
} // end of dbSelect function
} // end of class
and
include "myclass.php";
// create two new objects
$db1 = new mkaDB();
$db2 = new mkaDB();
// First Database Connection
$dbname1 = 'myfirstdatabase';
$db1->dbConnect($dbname1);
// Second Database Connection
$dbname2 = 'myseconddatabase';
$db2->dbConnect($dbname2);
$sql1 = "select * from mytable";
$db1->dbSelect($sql1);
$sql2 = "select * from myothertable";
$db2->dbSelect($sql2);
What I am trying to accomplish is creating 2 database connections, each connection to a different schema. I then want to be able to call each schema via the $db1->dbSelect
or $db2->dbSelect
. However, when I run this I get the "error" message from the dbSelect
function. If I block out all calls to the $db2
object, the $db1
object does work.
I thought I could use $this->dbConnection
to keep things sorted out, but this does not appear to be working.