Jump to content

mutiple database query using class


severndigital

Recommended Posts

so i wrote an dbconnection class

 

nothing fancy, just a few things to call different types of information.

 

however, I want to use it to connect to two different databases on after the other.

 

here is the code

$userInfo = new dbConnect();	
$accountInfo = $userInfo->returnArray("SELECT * FROM accounts WHERE name = 'JOE'");

//that works just fine. now i want to get information from a seperate database on a seperate server as the one above.

$account = new dbConnect();
$account->setInfo($accountInfo['dbaseHost'],$accountInfo['dbaseUser'],$accountInfo['dbasePass'],$accountInfo['dbaseTable']);
//then try a new call
$prodInfo = $account->returnArray("SELECT * FROM products");

 

the way my class is set up is the following, it's pretty big so i'll only include the relvant parts.

protected $_host 		= 'localhost';
protected $_username	= 'username';
protected $_password 	= 'password';
protected $_dbase		= 'dbase';

public function setInfo($dbHost,$dbUname,$dbPass,$dbBase)
{
	$this->_host = $dbHost;
	$this->_username = $dbUser;
	$this->_password = $dbPass;
	$this->_dbase = $dbBase;
}

private function connectDb()
{
	$connect = mysql_connect($this->_host,$this->_username,$this->_password);
	if(mysql_select_db($this->_dbase,$connect) ){
		return "connected";
	} else {
		return "disconnected";
	}
}


public function returnArray($sql)
{
	$returnArray = array();
	$dbConnect = $this->connectDb();	

	$pull = mysql_query($sql)or die('<b>Return Array Not Complete MySQL returned the following error:</b> ' . mysql_error());

	$total = mysql_num_rows($pull);

	foreach(mysql_fetch_array($pull,MYSQL_ASSOC) as $key=> $val){
		$returnArray[$key] = $val;
	}	
	return $returnArray;


}

 

when i try to instantiate the class a second time, it get two errors

 

1st error says that [email protected] using password"YES" denied. But i set the username and password using my class function.

 

2nd error says that the table from the second database does not exists in the first dbase call.

 

i am assuming the 2nd error is a result of the first, so why is the script trying to access as apache instead of the username i provided??

 

Thanks,

-C

Link to comment
https://forums.phpfreaks.com/topic/132294-mutiple-database-query-using-class/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.