Tigron Skeleton Mysql database layer
This library is a Mysqli wrapper with an easy-to-use API. Most basic features are taken care of automatically., (*1)
Installation via composer:, (*2)
composer require tigron/skeleton-database
Create a database connection:, (*3)
$dsn1 = 'mysqli://username:password@localhost/database'; $dsn2 = 'mysqli://username:password@localhost/database2'; $db = \Skeleton\Database\Database::Get($dsn1, true); // The second parameter makes this dsn default $db = \Skeleton\Database\Database::Get(); // returns a connection to dsn1 as this is default $db = \Skeleton\Database\Database::Get($dsn2); // returns a connection to dsn2, don't make it default $db = \Skeleton\Database\Database::Get(); // returns a connection to dsn1
Get a row of the resultset. The resultset should only contain 1 row, (*4)
$result = $db->get_row('SELECT * FROM user WHERE id=?', [ 1 ]); // Returns one row
Get a column, each element in the array contains the value of a row. The result should only contain 1 row, (*5)
$result = $db->get_column('SELECT id FROM user', []); // Returns 1 column
Get 1 field result., (*6)
$result = $db->get_one('SELECT username FROM user WHERE id=?', [ 1 ]); // Returns 1 field
Get all columns of a give table, (*7)
$result = $db->get_columns('user');
Insert data into a table, (*8)
$data = [ 'username' => 'testuser', 'firstname' => 'test', ]; $result = $db->insert('user', $data); // Inserts a new row
Update a row, (*9)
$data = [ 'username' => 'testuser', 'firstname' => 'test', ]; $where = 'id=' . $db->quote(1); $result = $db->update('user', $data, $where); // Updates a row
Run a query manually, (*10)
$result = $db->query('DELETE FROM `user` WHERE id=?', [ $user_id ]);
Clear all existing database connections, (*11)
Database::Reset();
For debug purpose flags can be set to see the queries, (*12)
\Skeleton\Database\Config::$query_log = false; // (default = false) \Skeleton\Database\Config::$query_counter = true; // (default = true) $database = \Skeleton\Database\Database::get(); print_r($database->query_log); print_r($database->query_counter);
Data quality insurance // Trim data if content is longer than table field \Skeleton\Database\Config::$auto_trim = false; // (default = false), (*13)
// Remove from objects properties which don't exist as table columns \Skeleton\Database\Config::$auto_discard = false; // (default = false) // Set to null every column which is not given as input and supports NULL values \Skeleton\Database\Config::$auto_null = false; // (default = false)