Getting a Connection
OpenAvanti’s database classes provide a mechanism for interacting with multiple databases and multiple database environments. This means you can access a wide array of database engines, such as PostgreSQL, MySQL and sqlite, and you can manipulate multiple databases in one project.
Establishing a Connection
Before we can actually query a database and manipulate its data, we must first connect and authenticate with that database.
OpenAvanti allows you to set up one or more database profiles that you provide to the database class. There are several pieces of data in this profile. Depending on the database type, some of these data may be optional:
- Profile Name: The name of the profile for later refererence. Only required if you have more than one connection.
- Driver: The driver name for the database type you are using. Example: postgres, mysql, sqlite, etc.
- Host: The database server hostname. Example: localhost, 127.0.0.1, db.myserver.com.
- Port: The database port to connect to on the specified host.
- Name: The name of the database you are connecting to.
- User: The name of the user with proper permission to interact with the database specified in name.
- Password: The password for the database user specified in user.
Piecing this together, we use the Database::AddProfile method to add our database profile:
<?php Database::AddProfile( array( "profile_name" => "main", "host" => "localhost", "driver" => "postgres", "name" => "mydatabase", "user" => "myuser", "password" => "supersecretpwd" ) ); ?>
A good place to put this connection string would be in the config.php file discussed in Creating a Configuration File.
Now to get a connection to the database in the form of a Database class object:
<?php $oDatabase = Database::GetConnection(); // Or, if you've added more than one profile: $oDatabase = Database::GetConnection( "your_profile_name" ); ?>
Now with this object, you can start running queries against the database.

