Simultaneous connection on straight seats PDO

Asked

Viewed 448 times

3

I need to make an appointment using two different banks and simultaneously. One is mysql and another firebird. It is already working separately, I would like to make the connection without having to close one to open the other.

Example

$sql_bd1 = connection::selectMysql('SELECT * FROM tabela_mysql');
$sql_bd2 = connection::selectIbase('SELECT * FROM tabela_firebird');

Connection class

<?php

class connection {

    public static $conn;

    public static function open($name) {
        if (file_exists("config/{$name}.ini")) {
            $db = parse_ini_file("config/{$name}.ini");
        } else {
            throw new exception("Arquivo '$name' nao encontrado");
        }

        $user = isset($db['user']) ? $db['user'] : NULL;
        $pass = isset($db['pass']) ? $db['pass'] : NULL;
        $name = isset($db['name']) ? $db['name'] : NULL;
        $host = isset($db['host']) ? $db['host'] : NULL;
        $type = isset($db['type']) ? $db['type'] : NULL;
        $port = isset($db['port']) ? $db['port'] : NULL;


        switch ($type) {
            case 'pgsql':
                $port = $port ? $port : '5432';
                $conn = new PDO("pgsql:dbname={$name}; user={$user}; password={$pass};host=$host;port={$port};");
                break;
            case 'mysql':
                $port = $port ? $port : '3306';
                $conn = new PDO("mysql:host={$host};port={$port};dbname={$name}", $user, $pass);
                break;
            case 'sqlite':
                $conn = new PDO("sqlite:{$name}");
                break;
            case 'ibase':
                $conn = new PDO("firebird:dbname={$name}", $user, $pass);
                break;
            case 'oci8':
                $conn = new PDO("oci:dbname={$name}", $user, $pass);
                break;
            case 'mssql':
                $conn = new PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
                break;
        }

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

        self::$conn = $conn;
    }

    public static function select($sql) {
        return self::$conn->query($sql);
    }

    public static function exec($sql, $ret_id = false) {
        $retorno = self::$conn->exec($sql);
        if ($ret_id)
            $retorno = self::$conn->lastInsertId();
        return $retorno;
    }

    public static function close() {
        self::$conn = null;
    }

}

1 answer

3


Your class is almost ready for this, just a small adjustment:

<?php

class connection {

    public static $conn;
    private static $last; // <--- parametro novo para controle da ultima conexão criada

    public static function open($basename) {
        if (file_exists("config/{$basename}.ini")) {
            $db = parse_ini_file("config/{$basename}.ini");
        } else {
            throw new exception("Arquivo '$basename' nao encontrado");
        }

        $user = isset($db['user']) ? $db['user'] : NULL;
        $pass = isset($db['pass']) ? $db['pass'] : NULL;
        $name = isset($db['name']) ? $db['name'] : NULL;
        $host = isset($db['host']) ? $db['host'] : NULL;
        $type = isset($db['type']) ? $db['type'] : NULL;
        $port = isset($db['port']) ? $db['port'] : NULL;


        switch ($type) {
            case 'pgsql':
                $port = $port ? $port : '5432';
                $conn = new PDO("pgsql:dbname={$name}; user={$user}; password={$pass};host=$host;port={$port};");
                break;
            case 'mysql':
                $port = $port ? $port : '3306';
                $conn = new PDO("mysql:host={$host};port={$port};dbname={$name}", $user, $pass);
                break;
            case 'sqlite':
                $conn = new PDO("sqlite:{$name}");
                break;
            case 'ibase':
                $conn = new PDO("firebird:dbname={$name}", $user, $pass);
                break;
            case 'oci8':
                $conn = new PDO("oci:dbname={$name}", $user, $pass);
                break;
            case 'mssql':
                $conn = new PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
                break;
        }

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

                   // |-----  torna $conn um array de conexões
        self::$conn[$basename] = $conn;
        self::$last = $basename;
    }

                                        // |-- parametro para recuperar a conexao desejada
    public static function select($sql, $base = null) {

            // |---- Se null retoma a ultima conexao criada
        if(!$base) $base = self::$last;

        return self::$conn[$base]->query($sql);
    }

    public static function exec($sql, $ret_id = false, $base = null) {

        if(!$base) $base = self::$last;

        $retorno = self::$conn[$base]->exec($sql);
        if ($ret_id)
            $retorno = self::$conn[$base]->lastInsertId();
        return $retorno;
    }

    public static function close($base = null) {

        if(!$base) $base = self::$last;

        self::$conn[$base] = null;
    }
}

To perform connection exchange simply add the connection parameter :

connection::select('SELECT * FROM tabela_mysql', 'mysql'); connection::select('SELECT * FROM tabela_firebird', 'oci8');

  • Opa blz, gave this error Notice: Undefined index: Ibase in /var/www/site/controller/Connection.class.php on line 61

  • @Eduardosantos I do not know which is the line 61 for you, but you started from the two connections? both from mysql and Ibase?

  • Yes, the line is that it has this Return self::$Conn[$base]->query($sql);

  • @Eduardosantos Edited, the variable $name was overwritten with the contents in the file, had not noticed it.

  • Now it worked. Thanks friend.

Browser other questions tagged

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