Switch to a different database. Does not work with SQLite or Postgres.
DB::useDB('my_other_database');
Get an array of the tables in the requested database. If no database is specified, use the current database.
$current_db_tables = DB::tableList();
$other_db_tables = DB::tableList('other_db');
foreach ($other_db_tables as $table) {
echo "Table Name: $table\n";
}
Get an array of the columns in the requested table in the current database.
$columns = DB::columnList('accounts');
foreach ($columns as $name => $details) {
echo "Column: $name\n";
echo "Type: " . $details['type'] . "\n";
echo "Null? " . $details['null'] . "\n";
echo "Key: " . $details['key'] . "\n";
echo "Default: " . $details['default'] . "\n";
echo "Extra: " . $details['extra'] . "\n";
}
Drop any existing MySQL connections. If you run a query after this, it will automatically reconnect.
DB::disconnect(); // drop connection
Return the underlying PDO object.
$pdo = DB::get();
The type of the current database we are connected to. Returns one of the following strings: mysql
, sqlite
, pgsql
.
$type = DB::dbType();
echo "We are connected to a $type database!";
Return the last query (whether or not it succeeded).
$query = DB::lastQuery();
Prepare a partial query, allowing it to be used as part of a larger query. Returns a result that can be passed as a parameter with %? or %l. This can be useful for building large queries out of pieces, except each piece can have its own parameters.
$part = DB::parse("WHERE name=%s AND age=%i", 'Joe', 15);
$results = DB::query("SELECT * FROM accounts %?", $part);
// SELECT * FROM accounts WHERE name='Joe' AND age=15
Change the character that appears before parameters in MeekroDB queries. The default is %
.
DB::$param_char = ':';
$results = DB::query("SELECT * FROM accounts WHERE email=:s", '[email protected]');
Change the character that separates the name for named parameters. The default is _
.
DB::$param_char = ':';
DB::$named_param_seperator = '@';
$results = DB::query("SELECT * FROM accounts WHERE email=:s@email", ['email' => '[email protected]']);
If it's been at least this long since our last query, re-connect to the database before running another query. Defaults to 4 hours, since MySQL's default wait_timeout is 8 hours.
// if MySQL was configured with a wait_timeout of 60 seconds, we should set
// our reconnect to 30 seconds like this
DB::$reconnect_after = 30;