connect in 2 banks with PDO

Asked

Viewed 410 times

2

Hello, I connect in a bank , how would I connect with another, which has the same login and password within the same server ? follows my function:

function conectaBanco() {

    $hostname='dbprovider.rede.local';
    $username='root';
    $password='8asd331221!';

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=Provider",$username,$password,
            array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        return $dbh;
    }

    catch(PDOException $e) {
        echo $e->getMessage();
    }
}

in the case as I do here to connect with the other bank, example I want to put

$dbh2 = new PDO("mysql:host=$hostname;dbname=Provider2",$username,$password,

but it didn’t work !!! anyone can help ? the error is Uncaught Error: Call to a Member Function query() on null line 48 and that line has

$dbh = conectaBanco();
  • 2

    Why "didn’t it work"? Did it give an error? Which one? By the way, it seems to be the same server, only Databases different, so why, instead of creating two connections, does not use the same with the use Provider2?

  • 1

    only creates separate functions one for each bank

  • 3

    I would create a function with a property and an internal switch or if, so standardizes and facilitates. Example: connDB('BD1'), connDB('BD2'), connDB('BD3'), etc Now the reason for the error, can be instance, user permission, wrong BD name, etc... If you do not put the error, can not guess.

  • @Very good idea, I think his mistake is that it is instantiating two connections and returning only one

  • 1

    the error is Uncaught Error: Call to a Member Function query() on null line 48 and on that line you have $dbh = connect();

  • I have to change everything to $dbh2 ?

  • attaches the error to the question and the code in question

  • 1

    @Rbz I will follow this function pattern for connection from now on, before I made one for each bank, great idea

  • @Rbz can give an example of what this function would look like ?

  • 1

    Posted. But remember, it’s basic, upon your code.

Show 5 more comments

1 answer

3


A basic example, on your question:

function conectaBanco($nomeBD) {

    switch ($nomeBD) {

        # Servidor 1, Banco de dados 1
        case 'BD1':
        $hostname='192.168.0.1';
        $dbname='banco1';
        $username='root';
        $password='xx1!';
        break;

        # Servidor 1, Banco de dados 2
        case 'BD2':
        $hostname='192.168.0.1';
        $dbname='banco2';
        $username='root';
        $password='xx1!';
        break;

        # Servidor 2, Banco de dados 1
        case 'BD3':
        $hostname='192.168.0.100';
        $dbname='banco3';
        $username='root';
        $password='yy2!';
        break;

        # Servidor 3, Banco de dados 1
        default:
        $hostname='dbprovider.rede.local';
        $dbname='bd_padrao';
        $username='root';
        $password='zz3!';
        break;
    }

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password,
            array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        return $dbh;
    }

    catch(PDOException $e) {
        echo $e->getMessage();
    }
}

Using the function:

$obj = new ClasseConexao;
$obj -> conectaBanco('BD2');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.