MeekroDB Quick Start Docs ORM Docs FAQ
Download GitHub

Connecting to a Database

Before you can run any queries, you must connect to a database. You must decide whether you're going to use MeekroDB in static method mode or object-oriented mode, and specify your database information.

Static vs Object Oriented Mode

Many projects will only ever talk to one database. In this case, it's easier to use MeekroDB's static mode, so that the database methods are easily available everywhere in your program without having to pass a $db object around. On the other hand, some projects would benefit from having $db objects that they can manage-- or you might be writing a library that needs to hold on to its own database connection without interfering with the parent project that's using your library. In this case, the object-oriented mode makes more sense.

Connecting in Static Mode

Before you run any SQL commands, you must set the variables below. MeekroDB won't actually establish a MySQL connection until you run your first command!

DB::$dsn = 'mysql:host=localhost;dbname=my_database_name';
DB::$user = 'my_database_user';
DB::$password = 'my_database_password';

// run your first query?
DB::query("SELECT * FROM tbl");

Connecting in Object-Oriented Mode

Obviously, you'll need to create the object before using it. MeekroDB won't actually establish a MySQL connection until you run your first command!

$dsn = 'mysql:host=localhost;dbname=my_database_name';
$user = 'my_database_user';
$password = 'my_database_password';
$db = new MeekroDB($dsn, $user, $password);

// run your first query?
$db->query("SELECT * FROM tbl");

The arguments to new MeerkoDB() are all optional. Any that are ommitted will be read from the current static instance of MeekroDB, or the defaults will be used. An assoc array of PDO attributes can optionally be passed as a 4th argument (see "SSL Connections" below for more on that).

What is a DSN?

A DSN is a string describing a database connection, in a format that has become standard for PHP. It has a prefix such as mysql:, and then a series of key=value pairs separated by semicolons. Valid keys for MySQL are: host, port, dbname, unix_socket, charset. Here are several example DSNs:

mysql:host=localhost;dbname=testdb
mysql:host=localhost;port=3307;dbname=testdb
mysql:unix_socket=/tmp/mysql.sock;dbname=testdb

SSL Connections

If you're connecting to MySQL over SSL, you may also need to set this as shown below. We have examples for both static and object-oriented mode, so use whichever one applies to you.

$connect_options = [
    PDO::MYSQL_ATTR_SSL_KEY => '',
    PDO::MYSQL_ATTR_SSL_CERT => '',
    PDO::MYSQL_ATTR_SSL_CA => '',
    PDO::MYSQL_ATTR_SSL_CAPATH => '',
];

// static mode
DB::$connect_options = $connect_options;

// object-oriented
$db = new MeekroDB($dsn, $user, $password, $connect_options);

The options here are imported directly from PHP's PDO library. You usually don't need to change them (except for SSL as shown here), but if you really want to, you can find a complete list of the generic ones here and the MySQL-specific ones here.

These settings only have an effect when a connection is being established. Once you're connected, changing them has no effect. If you really want to change them, you have to use DB::disconnect() so that the following query will auto-reconnect with your new settings.

Databases other than MySQL

In addition to MySQL, MeekroDB fully supports SQLite and Postgres. You can connect by setting the appropriate DSN, such as:

pgsql:host=localhost;port=5432;dbname=meekrodb
sqlite:
sqlite::memory:
sqlite:/opt/databases/mydb.sq3

MeekroDB has a few limitations with SQLite and Postgres. These are caused by how the databases themselves work, and therefore aren't something we could fix in a future release:

  • insertUpdate() and insertIgnore() don't work for SQLite until version 3.35.0
  • insertUpdate() and insertIgnore() don't work for Postgres
  • useDB() doesn't work for either SQLite or Postgres