Chris
Chris

Reputation: 5617

Use Two Database Connections through a PHP Class? (PHP / MySQL)

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.

Upvotes: 1

Views: 1570

Answers (3)

labue
labue

Reputation: 2623

You need the 4th, "new_link" parameter (php mysql_connect):

"If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters."

Upvotes: 0

Ian Taylor
Ian Taylor

Reputation: 179

Do you want something like this:

    class sql
{

    private static function _dbConnect()
    {
        mysql_connect("localhost", "uname", "pwd");
        mysql_select_db("db");
    }
private static function _dbConnect2()
    {
        mysql_connect("localhost", "uname", "pwd");
        mysql_select_db("db");
    }


public static function something($user){
            self::_dbConnect
           // code in here and mysql
          OR
            self::_dbConnect2

}

Something like that?

Worked example:

public static function getFriendNumber($username)
{
    self::_dbConnect();
    $query = mysql_query("SELECT friend_array FROM users WHERE `user_name` = '$username'")or die(mysql_error("Error selecting friend_array with id " . $username));
    while($row = mysql_fetch_array($query)){
        $friend_delimit = $row['friend_array']; 
    }
    if($friend_delimit != ''){
        $explosion = explode(",",$friend_delimit);
        $counted = count($explosion);
        return $counted;
    }
    else {
        $counted = '0';
        return $counted;
    }
}

Upvotes: 1

Churk
Churk

Reputation: 4637

your $this->dbConnection is not defined within your class.

class myDbClass {
  private $dbConnection
  ....
}

Upvotes: 0

Related Questions