¿Usar dos conexiones de base de datos a través de una clase PHP? (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

y

    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 objeto, el $db1 object does work.

Pensé que podría usar $this->dbConnection to keep things sorted out, but this does not appear to be working.

preguntado el 09 de marzo de 12 a las 16:03

3 Respuestas

Quieres algo como esto:

    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

}

¿Algo como eso?

Ejemplo resuelto:

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;
    }
}

respondido 09 mar '12, 16:03

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."

respondido 09 mar '12, 16:03

That did the trick. I changed $conn = mysql_connect($dbhost, $dbuser, $dbpass) a $conn = mysql_connect($dbhost, $dbuser, $dbpass, true) - Chris

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

class myDbClass {
  private $dbConnection
  ....
}

respondido 09 mar '12, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.