phputils/db

All database functions


License
MIT

Documentation

Installation

Just run composer install [package_name]

After that all these functions should be available in every PHP file (via composer's autoloader)

db_connect

@var \PDO $dbh *

function db_connect()

db_prepare_exec

Internal function to prepare and execute query (returns $sth)

function db_prepare_exec($query, $args, &$rv = NULL)

db_exec

Executes a insert/update query on database Returns last_insert_id if insert, row count otherwise

function db_exec($query, ...$args)

db_query

Executes a select query on database

function db_query($query, ...$args)

db_query_cached

Executes a query on database and caches the results

function db_query_cached(int $expiry, string $query, ...$args)

db_query_cached_single

Executes a query on database, caches the results (in file) and returns the first row

function db_query_cached_single(int $expiry, string $query, ...$args)

db_query_single

Executes a select query on database and returns the first rows

function db_query_single($query, ...$args)

db_query_value

Executes a select query on database and returns the first column of first row SELECT user_id from users limit 1; // returns user_id of first row

function db_query_value($query, ...$args)

db_query_values

Executes a select query on database and returns the first column of all rows as array SELECT user_id from users where name = 'san'; // returns all user_id where name = 'san'

function db_query_values($query, ...$args)

db_table_schema

Returns the table schema

function db_table_schema($table)

db_list_tables

Returns the list of tables in a database

function db_list_tables($cached = TRUE)

db_find

Finds a record

function db_find($table, $matches = [], $limit = 1)

db_find_cached

Finds a record using cache (local file system)

function db_find_cached(int $expiry, $table, $matches = [], $limit = 1)

db_find_cached_table

Finds a record using the cache table in current database

function db_find_cached_table($name, $value = NULL, $expires = '+1 day')

db_insert

Inserts a row into a table optionally filling out fields like created_at, updated_at, etc

function db_insert($table, array $values, $magic = FALSE, $insert_ignore = FALSE)

db_update

Updates matching rows optionally filling out fields like updated_at, etc

function db_update($table, array $matches, array $updates, $magic = FALSE, $update_ignore = FALSE, $limit = 1)

db_count

Returns the count of matching rows

function db_count($table, array $matches)

db_delete

Delets matching rows

function db_delete($table, array $matches, $limit = 1)

db_crud

Easy way to do CRUD operation on database

function db_crud($op, $table, $match, $updates = NULL, $failsafe = ['id', 'user_id'], $json_fields = [])

db_quote

Quotes a string using PDO

function db_quote($str)