Database connection - mhmxs/lisaframework GitHub Wiki

Database connection tutorial

Connecto to database

In Lisa default database type is MySQL. You can change default type in config/Config.ini. To get default database connection you can use system/libraries/Database/ConnectionFactory.php, but you can initialize other connections with system/libraries/Database/MySQL/DatabaseConnection.php class.

$connection = \Core\Database\ConnectionFactory::getDefaultConnection();
//or
$connection = \Core\Database\MySQL\DatabaseConnection::getConnection();

DatabaseConnection returns with default connectin in config/MySQL.ini, if you want to use another connection add it to the ini file like the default, add parameter to getConnection method.

$connection = \Core\Database\MySQL\DatabaseConnection::getConnection("other_connection");

Functions of a connection:

  • getAll - returns all hits from database
  • getOne - returns one row from database
  • execute - execute query
  • start - start transaction
  • commit - commit transaction
  • rollback - rollback transaction

Usage

getAll

$query = "SELECT * FROM table WHERE name = :name";
$prepare = array("name" => "Fred");
$results = \Core\Database\MySQL\DatabaseConnection::getConnection()->getAll($query, $prepare);

getOne

$result = \Core\Database\MySQL\DatabaseConnection::getConnection()->getOne($query, $prepare);

execute

\Core\Database\MySQL\DatabaseConnection::getConnection()->execute($query, $prepare);

start

If you start a transaction, don't commit it, the Transaction api rollback all changes when the transaction destroyed.

\Core\Database\MySQL\DatabaseConnection::getConnection()->start();

commit

\Core\Database\MySQL\DatabaseConnection::getConnection()->commit();

rollback

\Core\Database\MySQL\DatabaseConnection::getConnection()->rollback();