phppdofirebird

Link 2 Firebird databases with PDO


Short summary. I have 2 databases (Invoices.gdb and Users.gdb) I want to get a table where I can see which Invoice is linked to which customer.

For your info: I know the PHP code to get my values into the table. I'm struggling to get my LINK right between the 2 databases AND how I can execute this query.

My PDO class (only contains 1 connection right now)

class DatabasePDO{
    /** @var PDO */
    private static $dbh;

    static function getInstance(){
        $str_conn="firebird:host=localhost;dbname=C:/Users/Desktop/USERS.GDB;charset=UTF8";
        try{
            self::$dbh = new PDO($str_conn, "username", "password");
        }
        catch(PDOException $e){
            echo "Error!: ".$e->getMessage();
            die();
        }
        return self::$dbh;
    }

That's only to get my users(and it works). How can I add my Invoices DB?

This is my class

public static function getInvoices() {
        $conn = DatabasePDO::getInstance();
        $sql = "select * from users.customers as A , invoices.invoice as B
                where a.customers.customerid = b.invoice.customerid";
        $st = $conn->prepare($sql);
        $st->execute();
        $list = array();
        while ( $row = $st->fetch() ) {
            $invoice= new invoice( $row );
            $list[] = $invoice;
        }
        $conn = null;
        return $list;
    }

The query I want to make is this the following:

select * from users.customers as A , invoices.invoice as B
where a.customers.customerid = b.invoice.customerid

I can't merge the 2 databases together. So that's no solution. Thanks in advance!


Solution

  • Firebird databases are isolated and you can't join between databases. The best solution would be to merge your two databases into one (that would also have the added benefit of having integrity checks like foreign key constraints, etc).

    The second best would be to have a copy of your users database (or at least the required tables) in your invoices database. You would then need to synchronize changes in users to invoices (eg using triggers and EXECUTE STATEMENT ON EXTERNAL, or a scheduled to job to do the synchronization).

    Otherwise you will have to do joining by hand in your code, and this is far from optimal.