Back

Database

Overview

Here you find all the functions you need to interact with the database. The connection to your database is initialized automatically. Just use these functions to insert, update and receive data from your database.

All database functions

$db->update( $table, $data, $where ); $db->insert( $table, $data ); $row = $db->getByName( $table, $name ); $row = $db->getById( $table, $id ); $row = $db->get( $table, $where ); $row = $db->getByQuery( $sql, $args ); $array = $db->getAll( $table, $where ); $array = $db->getAllByQuery( $sql, $args ); $row = $db->query( $sql, $args );
  • The data is delivered in an array, you can access your row like this $userRow['username']

Fetch a single database row

Here you find all the functions you need to interact with the database. The connection to your database is initialized automatically. Just use these functions to insert, update and receive data from your database.

Fetch a database row from the user-table with the username jay:

$userRow = $db->get( 'user', array( 'username' => 'jay' ) ); echo $userRow['email']; // print the users email address.

Fetch a database row from the user-table with the username jay:

$userRow = $db->get( 'user', array( 'username' => 'jay' ) );

Fetch a row by the name column of the user-table:

$userRow = $db->getByName( 'user', 'joel' );

Instead of hard-coding ID's in your PHP-code, it is recommended to add an additional name-column to identify the row. You can not query like this other columns.

Fetch a row by the id column of the user-table:

$userRow = $db->getById( 'user', 21 );

Fetch a row with a custom MySQL-query:

$db->getByQuery('SELECT * FROM house WHERE floors=:floors', array('floors' => 4) );

The data in the methods above is always delivered in an array, you can access your row like this $userRow['username']

Fetch multiple rows from a database table

Use the database insert function to add a new row in a table.

Insert a new row into the address table:

$where = array( 'status' => 'active', 'type' => 'admin' ); $array = $db->getAll( 'user', $where );

This query would fetch all users which are saved as type admin and with the status active.

Fetch multiple rows by a custom MySQL query

Fetch all rows from the user table where status is active:

$array = $db->getAllByQuery( "SELECT * FROM user WHERE status=:status", array('status' => 'active') ); foreach( $array as $databaseRow ) { echo $databaseRow['firstname']; }

Insert a row into the database

Use the database insert function to add a new row in a table.

Insert a new row into the address table:

$addressData = array( 'street' => 'Mainstreet 3', 'zipcode' => '21900', 'country' => 'us' ); $db->insert( 'address', $addressData );

Update a row in a database table

Use the update function to update an existing row in a database table.

Insert a new row into the address table:

$addressData = array( 'name' => 'Mainstreet 3', 'zipcode' => '21900', 'country' => 'us' ); $where = array( 'id' => 12 ); // Which rows should be updated $db->update( 'address', $addressData, $where );

Custom MySQL query

Just use the query function to execute any custom MySQL query.

Insert a new row into the address table:

$arguments = array( 'country' => 'us' ); $row = $db->query( "SELECT * FROM address WHERE country=:country", $arguments );