PHP Класс db, kirby

Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$trace array Traces all db queries

Открытые методы

Метод Описание
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

Описание методов

affected() статический публичный метод

Returns the number of affected rows for the last query
static public affected ( ) : integer
Результат integer

charset() статический публичный метод

Sets the charset for all queries The default and recommended charset is utf8
static public charset ( string $charset = 'utf8' ) : mixed
$charset string
Результат mixed

column() статический публичный метод

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
Результат mixed

connect() статический публичный метод

It will only connect once and return that same connection for all following queries
static public connect ( ) : mixed
Результат mixed

connection() статический публичный метод

Returns the current connection or false
static public connection ( ) : mixed
Результат mixed

count() статический публичный метод

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
Результат integer

database() статический публичный метод

Selects a database
static public database ( string $database ) : mixed
$database string
Результат mixed

delete() статический публичный метод

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
Результат mixed The number of affected rows or an error response

disconnect() статический публичный метод

Disconnects from the server
static public disconnect ( ) : boolean
Результат boolean

error() статический публичный метод

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?
Результат mixed

escape() статический публичный метод

Escapes unwanted stuff in values like slashes, etc.
static public escape ( string $value ) : string
$value string
Результат string Returns the escaped string

execute() статический публичный метод

This is used for queries like update, delete or insert
static public execute ( string $sql ) : mixed
$sql string The sql query
Результат mixed

fetch() статический публичный метод

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
Результат array The key/value result array

field() статический публичный метод

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"
Результат mixed

fields() статический публичный метод

Returns an array of fields in a given table
static public fields ( string $table ) : array
$table string The table name
Результат array The array of field names

in() статический публичный метод

A simplifier to build IN clauses
static public in ( array $array ) : string
$array array An array of fieldnames
Результат string The MySQL string for the where clause

insert() статический публичный метод

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
Результат mixed The last inserted id if everything went fine or an error response.

insert_all() статический публичный метод

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.
Результат mixed The last inserted id if everything went fine or an error response.

join() статический публичный метод

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)
Результат mixed

last_id() статический публичный метод

Returns the last returned insert id
static public last_id ( ) : integer
Результат integer

left_join() статический публичный метод

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
Результат mixed

max() статический публичный метод

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
Результат mixed

min() статический публичный метод

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
Результат mixed

prefix() статический публичный метод

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
Результат string The sanitized table name.

query() статический публичный метод

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
Результат mixed

replace() статический публичный метод

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
Результат mixed The last inserted id if everything went fine or an error response.

row() статический публичный метод

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"
Результат mixed

search_clause() статический публичный метод

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
Результат string Returns the final where clause

select() статический публичный метод

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()
Результат mixed

select_clause() статический публичный метод

Builds a select clause from a simple array
static public select_clause ( $fields ) : string
Результат string The MySQL string

simple_fields() статический публичный метод

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
Результат array The result array without those damn prefixes.

sum() статический публичный метод

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
Результат mixed

update() статический публичный метод

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
Результат mixed The number of affected rows or an error response

values() статический публичный метод

Makes it possible to use arrays for inputs instead of MySQL strings
static public values ( array $input ) : string
$input array
Результат string The final MySQL string, which will be used in the queries.

where() статический публичный метод

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
Результат string The MySQL string for the where clause

with() статический публичный метод

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
Результат string Returns the where clause part

Описание свойств

$trace публичное статическое свойство

Traces all db queries
public static array $trace
Результат array