What is the best connection method between PHP and Firebird?

Asked

Viewed 963 times

-1

I use the functions ibase_ to connect and manipulate data on Firebird with the PHP. I saw that many people also use pdo_firebird. The version of Firebird that I use is 2.5. After all, I am using the best and most viable method of manipulating the data or it would be better to switch to pdo?

This doubt arose these days for having researched so much about some problems in the Firebird and I saw that most of the posts I researched mention the pdo. Also by the fact that ibase_ could be better for use with the Interbase, since its name already says.

  • I use pdo_firebird and works very well, I have a class that makes the connection with the same.

  • Eduardo, could you post the code of this class? And you know about performance and feasibility if it is better to use the pdo?

  • Yes, about performance I can’t tell you because I’ve never used otherwise.

1 answer

1

Follow the class I have:

Connection.class.php

    <?php

    class Connection {

        public static $conn;

        //lê o arquivo .ini com as config

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

    }

Ibase.ini

host = 
name = localhost/dados/banco.fdb
user = SYSDBA
pass = 1234
type = ibase

The class call would look like this:

Connection::open('ibase');

$sql = Connection::select('SELECT * FROM banco');//Select

$sql = Connection::exec('INSERT INTO banco (campo) VALUES ('Teste')');//Insert

Connection::close('ibase');

Don’t forget to add the package below:

inserir a descrição da imagem aqui

  • Thank you friend for the availability and dedication in your reply. I’m going to do tests by moving to this class, because I’m having little problems when it comes to receiving the exceptions created in the database using ibase_.

  • 1

    I believe it will help you, I have Firebird 1.5 of an old system, and the queries work very well.

Browser other questions tagged

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