Description
Core\Db db()
Access our database layer with the db() function. It supports method chaining.
Examples
<?php
// Run a query to get at least 10 users
$users = db()->select('*')
->from(':user')
->order('user_id ASC')
->limit(10)
->executeRows();
// Get a single user
$user = db()->select('*')->from(':user')->where(['user_id' => 1])->executeRow();
// Insert an entry into the DB
$insert_id = db()->insert(':user', ['user_name' => 'Foo']);
// Update an entry in the DB
db()->update(':user', ['user_name' => 'bar'], ['user_id' => 1]);
// Get the total rows
$total = db()->select('COUNT(*)')->from(':user')->count();
// Joining tables
db()->select('f.*, b.*')
->from(':foo', 'f')
->join(':bar', 'b', 'b.id = f.id')
->executeRow();
Methods
| Method | Description | Usage | Returns |
|---|---|---|---|
select(string $select)
$select SQL Select | Select statement for sql query | // Method chaining
->select('user_id, user_name')
// SQL equivalent
SELECT user_id, user_name
| object Core\Db |
from(string $table [, string $alias] )
$table Database table name. $alias (optional) Table alias. | Define the table in your SELECT query. Table Prefix: Be sure to prefix tables with a colon. | // Method chaining
->from(':user', 'u')
// SQL equivalent
FROM prefix_user AS u
| object Core\Db |
where(array $condition)
$condition SQL condition in an ARRAY format. | Associative array of a conditional statement. Learn more here. | // Method chaining ->where([ 'user_name' => 'Foo', 'full_name' => 'Bar' ]) // SQL equivalent WHERE user_name = "Foo" AND full_name = "Bar" | object Core\Db |
Conditional Array Statements
...