PHP Class db, kirby

Show file Open project: bastianallgeier/kirby Class Usage Examples

Public Properties

Property Type Description
$trace array Traces all db queries

Public Methods

Method Description
affected ( ) : integer Returns the number of affected rows for the last query
charset ( string $charset = 'utf8' ) : mixed Sets the charset for all queries The default and recommended charset is utf8
column ( string $table, string $column, mixed $where = null, string $order = null, integer $page = null, integer $limit = null ) : mixed Returns all values from single column of a table
connect ( ) : mixed The core connection method Tries to connect to the server Selects the database and sets the charset
connection ( ) : mixed Returns the current connection or false
count ( string $table, mixed $where = '' ) : integer Counts a number of rows in a table
database ( string $database ) : mixed Selects a database
delete ( string $table, mixed $where = '' ) : mixed Runs a DELETE query
disconnect ( ) : boolean Disconnects from the server
error ( string $msg = null, boolean $exit = false ) : mixed An internal error handler
escape ( string $value ) : string Escapes unwanted stuff in values like slashes, etc.
execute ( string $sql ) : mixed Executes a MySQL query without result set.
fetch ( resource $result, const $type = MYSQL_ASSOC ) : array Shortcut for mysql_fetch_array
field ( string $table, string $field, mixed $where = null, string $order = null ) : mixed Returns a single field value from a table
fields ( string $table ) : array Returns an array of fields in a given table
in ( array $array ) : string A simplifier to build IN clauses
insert ( string $table, mixed $input, boolean $ignore = false ) : mixed Runs a INSERT query
insert_all ( string $table, array $fields, array $values ) : mixed Runs a INSERT query with values
join ( string $table_1, string $table_2, string $on, mixed $select, mixed $where = null, string $order = null, integer $page = null, integer $limit = null, string $type = "JOIN" ) : mixed Joins two tables and returns data from them
last_id ( ) : integer Returns the last returned insert id
left_join ( string $table_1, string $table_2, string $on, mixed $select, mixed $where = null, string $order = null, integer $page = null, integer $limit = null ) : mixed Runs a LEFT JOIN
max ( string $table, string $column, mixed $where = null ) : mixed Gets the maximum value in a column of a table
min ( string $table, string $column, mixed $where = null ) : mixed Gets the minimum value in a column of a table
prefix ( string $table ) : string Adds a prefix to a table name if set in c::set('db.prefix', 'myprefix_'); This makes it possible to use table names in all methods without prefix and it will still be applied automatically.
query ( string $sql, boolean $fetch = true ) : mixed Runs a MySQL query.
replace ( string $table, mixed $input ) : mixed Runs a REPLACE query
row ( string $table, mixed $select = '*', mixed $where = null, string $order = null ) : mixed Returns a single row from a table
search_clause ( string $search, array $fields, string $mode = 'OR' ) : string A simplifier to build search clauses
select ( string $table, mixed $select = '*', mixed $where = null, string $order = null, integer $page = null, integer $limit = null, boolean $fetch = true ) : mixed Returns multiple rows from a table
select_clause ( $fields ) : string Builds a select clause from a simple array
simple_fields ( array $array ) : array Strips table specific column prefixes from the result array
sum ( string $table, string $column, mixed $where = null ) : mixed Gets the sum of values in a column of a table
update ( string $table, mixed $input, mixed $where ) : mixed Runs an UPDATE query
values ( array $input ) : string Makes it possible to use arrays for inputs instead of MySQL strings
where ( array $array, string $method = 'AND' ) : string A handler to convert key/value arrays to an where clause
with ( string $field, string $char ) : string An easy method to build a part of the where clause to find stuff by its first character

Method Details

affected() static public method

Returns the number of affected rows for the last query
static public affected ( ) : integer
return integer

charset() static public method

Sets the charset for all queries The default and recommended charset is utf8
static public charset ( string $charset = 'utf8' ) : mixed
$charset string
return mixed

column() static public method

Returns all values from single column of a table
static public column ( string $table, string $column, mixed $where = null, string $order = null, integer $page = null, integer $limit = null ) : mixed
$table string The table name
$column string The name of the column
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
$order string Order clause without the order keyword. ie: "added desc"
$page integer a page number
$limit integer a number for rows to return
return mixed

connect() static public method

It will only connect once and return that same connection for all following queries
static public connect ( ) : mixed
return mixed

connection() static public method

Returns the current connection or false
static public connection ( ) : mixed
return mixed

count() static public method

Counts a number of rows in a table
static public count ( string $table, mixed $where = '' ) : integer
$table string The table name
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return integer

database() static public method

Selects a database
static public database ( string $database ) : mixed
$database string
return mixed

delete() static public method

Runs a DELETE query
static public delete ( string $table, mixed $where = '' ) : mixed
$table string The table name
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return mixed The number of affected rows or an error response

disconnect() static public method

Disconnects from the server
static public disconnect ( ) : boolean
return boolean

error() static public method

An internal error handler
static public error ( string $msg = null, boolean $exit = false ) : mixed
$msg string The error/success message to return
$exit boolean die after this error?
return mixed

escape() static public method

Escapes unwanted stuff in values like slashes, etc.
static public escape ( string $value ) : string
$value string
return string Returns the escaped string

execute() static public method

This is used for queries like update, delete or insert
static public execute ( string $sql ) : mixed
$sql string The sql query
return mixed

fetch() static public method

Shortcut for mysql_fetch_array
static public fetch ( resource $result, const $type = MYSQL_ASSOC ) : array
$result resource the unfetched result from db::query()
$type const PHP flag for mysql_fetch_array
return array The key/value result array

field() static public method

Returns a single field value from a table
static public field ( string $table, string $field, mixed $where = null, string $order = null ) : mixed
$table string The table name
$field string The name of the field
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
$order string Order clause without the order keyword. ie: "added desc"
return mixed

fields() static public method

Returns an array of fields in a given table
static public fields ( string $table ) : array
$table string The table name
return array The array of field names

in() static public method

A simplifier to build IN clauses
static public in ( array $array ) : string
$array array An array of fieldnames
return string The MySQL string for the where clause

insert() static public method

Runs a INSERT query
static public insert ( string $table, mixed $input, boolean $ignore = false ) : mixed
$table string The table name
$input mixed Either a key/value array or a valid MySQL insert string
$ignore boolean Set this to true to ignore duplicates
return mixed The last inserted id if everything went fine or an error response.

insert_all() static public method

Runs a INSERT query with values
static public insert_all ( string $table, array $fields, array $values ) : mixed
$table string The table name
$fields array an array of field names
$values array an array of array of keys and values.
return mixed The last inserted id if everything went fine or an error response.

join() static public method

Joins two tables and returns data from them
static public join ( string $table_1, string $table_2, string $on, mixed $select, mixed $where = null, string $order = null, integer $page = null, integer $limit = null, string $type = "JOIN" ) : mixed
$table_1 string The table name of the first table
$table_2 string The table name of the second table
$on string The MySQL ON clause without the ON keyword. ie: "user_id = comment_user"
$select mixed Either an array of fields or a MySQL string of fields
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
$order string Order clause without the order keyword. ie: "added desc"
$page integer a page number
$limit integer a number for rows to return
$type string The join type (JOIN, LEFT, RIGHT, INNER)
return mixed

last_id() static public method

Returns the last returned insert id
static public last_id ( ) : integer
return integer

left_join() static public method

Runs a LEFT JOIN
static public left_join ( string $table_1, string $table_2, string $on, mixed $select, mixed $where = null, string $order = null, integer $page = null, integer $limit = null ) : mixed
$table_1 string The table name of the first table
$table_2 string The table name of the second table
$on string The MySQL ON clause without the ON keyword. ie: "user_id = comment_user"
$select mixed Either an array of fields or a MySQL string of fields
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
$order string Order clause without the order keyword. ie: "added desc"
$page integer a page number
$limit integer a number for rows to return
return mixed

max() static public method

Gets the maximum value in a column of a table
static public max ( string $table, string $column, mixed $where = null ) : mixed
$table string The table name
$column string The name of the column
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return mixed

min() static public method

Gets the minimum value in a column of a table
static public min ( string $table, string $column, mixed $where = null ) : mixed
$table string The table name
$column string The name of the column
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return mixed

prefix() static public method

Adds a prefix to a table name if set in c::set('db.prefix', 'myprefix_'); This makes it possible to use table names in all methods without prefix and it will still be applied automatically.
static public prefix ( string $table ) : string
$table string The name of the table with or without prefix
return string The sanitized table name.

query() static public method

You can use any valid MySQL query here. This is also the fallback method if you can't use one of the provided shortcut methods from this class.
static public query ( string $sql, boolean $fetch = true ) : mixed
$sql string The sql query
$fetch boolean True: apply db::fetch to the result, false: go without db::fetch
return mixed

replace() static public method

Runs a REPLACE query
static public replace ( string $table, mixed $input ) : mixed
$table string The table name
$input mixed Either a key/value array or a valid MySQL insert string
return mixed The last inserted id if everything went fine or an error response.

row() static public method

Returns a single row from a table
static public row ( string $table, mixed $select = '*', mixed $where = null, string $order = null ) : mixed
$table string The table name
$select mixed Either an array of fields or a MySQL string of fields
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
$order string Order clause without the order keyword. ie: "added desc"
return mixed

search_clause() static public method

A simplifier to build search clauses
static public search_clause ( string $search, array $fields, string $mode = 'OR' ) : string
$search string The search word
$fields array An array of fields to search
$mode string OR or AND
return string Returns the final where clause

select() static public method

Returns multiple rows from a table
static public select ( string $table, mixed $select = '*', mixed $where = null, string $order = null, integer $page = null, integer $limit = null, boolean $fetch = true ) : mixed
$table string The table name
$select mixed Either an array of fields or a MySQL string of fields
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
$order string Order clause without the order keyword. ie: "added desc"
$page integer a page number
$limit integer a number for rows to return
$fetch boolean true: apply db::fetch(), false: don't apply db::fetch()
return mixed

select_clause() static public method

Builds a select clause from a simple array
static public select_clause ( $fields ) : string
return string The MySQL string

simple_fields() static public method

If you use column names like user_username, user_id, etc. use this method on the result array to strip user_ of all fields
static public simple_fields ( array $array ) : array
$array array The result array
return array The result array without those damn prefixes.

sum() static public method

Gets the sum of values in a column of a table
static public sum ( string $table, string $column, mixed $where = null ) : mixed
$table string The table name
$column string The name of the column
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return mixed

update() static public method

Runs an UPDATE query
static public update ( string $table, mixed $input, mixed $where ) : mixed
$table string The table name
$input mixed Either a key/value array or a valid MySQL insert string
$where mixed Either a key/value array as AND connected where clause or a simple MySQL where clause string
return mixed The number of affected rows or an error response

values() static public method

Makes it possible to use arrays for inputs instead of MySQL strings
static public values ( array $input ) : string
$input array
return string The final MySQL string, which will be used in the queries.

where() static public method

A handler to convert key/value arrays to an where clause
static public where ( array $array, string $method = 'AND' ) : string
$array array keys/values for the where clause
$method string AND or OR
return string The MySQL string for the where clause

with() static public method

An easy method to build a part of the where clause to find stuff by its first character
static public with ( string $field, string $char ) : string
$field string The name of the field
$char string The character to search for
return string Returns the where clause part

Property Details

$trace public static property

Traces all db queries
public static array $trace
return array