Database::initialize();The ability to fetch data from a database into a nicely formatted associative array is one of the main features of this library.
Fetch all of the rows from a database:
Database::select('Users', '*');Fetch some rows from a database:
Database::select(
'Users',
'*',
array(
'limit' => '0,5',
'orderBy' => 'last_name ASC,first_name ASC' ) );Getting the number of rows from the previous result
Database::numrows();Fetch a single row:
Database::select(
'Users',
'first_name,last_name,user_email',
array(
'where' => array(
'uid' => 110 ),
'singleRow' => 'true' ) );Fetch a single column:
Database::select(
'Users',
'user_email',
array(
'orderBy' => 'last_name ASC,first_name ASC',
'fetchStyle' => 'singleColumn' ) );Fetch a single value:
Database::select(
'Users',
'first_name',
array(
'where' => array(
'uid' => 396 ),
'single' => true ) );Database::insert(
'Users',
array(
'first_name' => 'Abe',
'last_name' => 'Lincoln' ) );Get the last inserted id:
Database::lastInsertId();Database::update(
'Users',
array(
'uid' => $lastInsertId,
'first_name' => 'George',
'last_name' => 'Washington' ),
array(
'uid' ) );Database::delete(
'Users',
array(
'uid' => $lastInsertId ) );Any SQL statement can be executed. In the following example we will optimize all of the tables in the database.
$tables = Database::listTables();
foreach( $tables as $table )
Database::sql( "OPTIMIZE TABLE `$table`;" );