Wrapper for PHP' pdo driver.
This packages comes from swayengine 2.
composer require rev/sway-dblayer
This will install package at latest version.
This packages requires PDO driver (for default, bundled with PHP).
- Create connection handler
<?php
/**
* Helps to create connection credentials
*/
$dblayerConnector = new DBLayerConnector();
$dblayerConnector->setDatabaseHostName('localhost');
$dblayerConnector->setDatabaseListenerPort(3306);
$dblayerConnector->setDatabaseName('my_db');
$dblayerConnector->setDatabaseUserName('john_sky');
$dblayerConnector->setUserPassword('my_secret_password');
$dblayerConnector->setSchemaPrefix('dev');
/**
* Sets to 'pdo_mysql'
*/
$dblayerConnector->useMysqlDriver();
/**
* Connects to database and get DBLayer instance
*/
$db = $dblayerConnector->connect();
?>
- Fetching rows
<?php
/**
* Returns all rows with given type as associative array
*/
$db->Run("SELECT * FROM %pr%my_table WHERE type = ?",
[ 'eg_type', PDO::PARAM_STR])->assoc();
/**
* Returns first fetched row as associative array
*/
$db->Run("SELECT * FROM %pr%my_table WHERE type = ?",
[ 'eg_type', PDO::PARAM_STR ])->assoc(0);
/**
* Returns value under column 'column1' at first fetched row.
*/
$db->Run("SELECT column1, column_2 FROM %pr%my_table WHERE type = ?",
[ 'eg_type', PDO::PARAM_STR ])->assoc(0, 'column1');
/**
* Executes an query and pass array with fetched rows to given anonymous function.
* Anonymous function must returns a value.
*/
$db->Run("SELECT * FROM %pr%my_table WHERE type = ?",
[ 'eg_type', PDO::PARAM_STR ])->assoc(function($entries) {
//do something with fetched entries
return $entries;
});
?>
- Inserting, updating, removing rows
<?php
$db = (new DBLayerConnector())->connect();
/**
* Returns True if rows have been updated,
* returns false if no rows have been updated.
*/
$db->Run("UPDATE %pr%my_table SET column1 = ? WHERE type = ?",
[ 'value1', PDO::PARAM_STR ],
[ 'eg_type', PDO::PARAM_STR ])->assoc();
?>