Database Connection - ApinePHP/legacy-framework GitHub Wiki

APIne Framework requires access to a database for its basic modules like sessions to work. It comes with a database utility whose goal is to ease off management of connections to database and provides minimal interfaces to simplify the interaction with the database. For full documentation on the utility, check the page about the Database in the Utility section.

The Database Utility currently only supports connections using a MySQL compatible syntax.

The Database Utility uses the PDO extension. Therefore PDO must be installed with the required drivers and being active. It normally is as of PHP 5.4.

Usage

First, you must make sure to include the Database class.

use Apine\Core\Database;

Fetch Data

The select method returns an array with every matching rows.

$database = new Database();
$result = $database->select(
    'SELECT * from apine_users WHERE `register_date` > "2015-08"'
);

Insert Data

The insert method requires the table name and an associative array of each field and their values. The insertion returns the id of the newly inserted row.

$database = new Database();
$result = $database->insert("apine_users", array(
    "username" => "Youmy001",
    "password" => "HASHEDPASSWORD",
    "email" => "[email protected]"
));

Update Data

The update method requires three arguments : a table name, a condition associative array and an associative array with new values.

$database = new Database();
$result = $database->update("apine_users", array(
    "email" => "[email protected]"
), array("id" => 5));

Delete Data

The row deletion requires a table name and a condition associative array. It returns true on success.

$database = new Database();
$result = $database->delete("apine_users", array(
    "id" => 5
));