Install
Download library:
wget https://mysqly.com/mysqly.php
and use it in your app:
|
|
Quick start
|
|
|
Insert
Insert single row from associative array
The mysqly::insert()
method will securely insert given data and return last inserted id
(if there is id
auto_increment column):
|
|
|
Ignore duplicates on insert
Setting 3rd argument to true
will ignore duplicates:
|
|
|
Insert or update on duplicate keys
The mysqly::insert_update()
methods allows to execute INSERT...ON DUPLICATE KEY UPDATE
query based on a given row:
|
|
|
Bulk multiple rows insert
The mysqly::multi_insert()
method will execute optimized bulk insert of multiple rows:
|
|
|
Data retrieval using parametric queries
Simple filtering based on column values
The mysqly::fetch()
method gets data from Mysql based on specified filters and returns list of associative arrays.
Given parameters (column values) are automatically escaped based on secure binding and stored procedures.
|
|
|
Sorting with parametric queries
Use 'order_by'
parameter to specify sort expression:
|
|
|
Multiple-values parameters
Use arrays as parameter values to filter based on multiple values:
|
|
|
Select data using native SQL
Plain SQL queries
The mysqly::fetch()
method can also accept native SQL queries.
|
|
|
Binding values and stored procedures
Use values binding to secure SQL queries:
|
|
|
"IN" queries binding support
Array values in bindings are supported for SELECT ... IN ...
queries:
|
|
|
Single rows and scalar values
Use mysqly::row()
to retrieve single table row or mysqly::one()
to get single scalar value:
|
|
Using cursors for large data volumes
Use mysqly::fetch_cursor()
method to work directly with PDO cursor:
|
|
Native SQL queries are also supported:
|
|
Lists and key-values retrieval
Retrieve lists
The mysqly::array()
method will return list of first column values from a result set.
|
|
|
Key-value pairs retrieval
The mysqly::key_vals()
method will return associative (key-value) array of 1st and 2nd columns from result set respectively:
|
|
|
Data retrieval based on dynamic methods
Get rows from table by parametric filters
Use mysqly::{table}($filters)
method to get rows based on filters:
|
|
|
Get single row from table by "ID" column
Use mysqly::{table}_($id)
method to get row based on specified value of ID
column (should be numeric value):
|
|
|
Get single column value by paremetric filter
Use mysqly::{table}_{column}($filters)
method to get single column
value:
|
|
|
If you pass numeric value, this will be treated as id
filter:
|
|
|
Aggregations and random data
Count by parametric filters
Use mysqly::count()
method to count rows by filters:
|
|
|
Min/max/avg column values
Use mysqly::min_{column}()
, mysqly::max_{column}()
and mysqly::avg_{column}()
methods
to get corresponding aggregate column value based on filters:
|
|
|
Random row based on filters:
Use mysqly::random()
method to get single random row from a table based on filters:
|
|
|
Atomic increment/decrement
Use mysqly::increment()
method to atomically increment specified column values based on filters:
|
|
|
And mysqly::decrement()
method to atomically decrease value:
|
|
|
Update data
Use mysqly::update()
method to update rows with specified data based on filters:
|
|
|
Remove data
Use mysqly::remove()
method to remove rows from table based on filters:
|
|
|
Custom queries execution
Use mysqly::exec()
to execute custom queries that don't return values:
|
|
Transactions
Use mysqly::transaction()
method to execute transactions:
|
|
|
Return true
from callback to commit transaction:
|
|
|
Advanced authentication
Basic Mysql server authentication:
|
|
System-wide credentials
You can create /var/lib/mysqly/.auth.php
file with credentials that will be automatically used to authenticate library:
|
Multiple DB/server connections
In order to use multiple connections, add child class for every connection:
|
|
Working with JSON
Automatically retrieve JSON columns as associative arrays
To automatically retrieve JSON data as associative arrays, append _json
postfix to column names (or aliases):
|
|
|
Updating JSON attributes
Use dots (.
) in column names to automatically treat them as JSON:
|
|
|
Auto table/field creation mode
Mysqly supports automatic table creation/alter mode (useful on early stages of prototyping).
Enable table/field automatic creation
Use mysqly::auto_create()
method to enable or disable automatic creation/alter:
|
|
Using automatic table/field creation
When auto_create mode is on,
mysqly::insert()
, mysqly::update()
and mysqly::insert_update()
methods will automatically create absent tables or fields.
Columns named id
will be created as BIGINT PRIMARY KEY
while
all other columns will be created with TEXT
type.
|
|
Key-value storage
Mysqly includes set of methods to work with built-in key/value storage on top of Mysql Engine. Key-value storage is an InnoDB table, which is automatically created upon first request.
Settings values
Use mysqly::set()
method to set values by keys:
|
|
|
Getting values
Use mysqly::get()
method to get values by keys:
|
|
|
Using multiple key-value storages
Specify space name as last argument for mysqly::get()
and mysqly::set()
methods to use multiple separate key-value storages:
|
|
|
Read more about implementing key-value storage on top of Mysql.
Job queue
Job queue component allows queuing events with additonal data and then reading from this queue in a FIFO order.
Queue is implemented on top of InnoDB table (named _queue
, automatically created upon first request) and uses locks to allow atomicity.
Writing job to queue
Use mysqly::write()
method to add event to queue:
|
|
Reading jobs from queue
Use mysqly::read()
method to read next job from queue by event type:
|
|
Subscribing to queue events
Use mysqly::on()
method to subscribe to queue by event type.
Warning.
This method will lock code execution and wait for new events from queue (useful for background scripts):
|
|
Read about implementation of job queue on top of Mysql InnoDB engine.
Cache storage with TTL support
Cache component allows using cache storage on top of InnoDB table.
_cache
table will be atomatically created upon first cache request.
Warning! Cache table is not cleared automatically, so it is recommended to delete
old data from _cache
table periodically (by cron).
Caching and getting cached data
Unlike usual implementation (get/set methods), Mysqly implements single mysqly::cache()
method,
that will automatically return cached value or get it from source (through callback):
|
|
Remove cached data
Use mysqly::uncache()
method to remove cached data by key:
|
|
Read more about implementing cache storage on top of Mysql.
Exporting data to CSV/TSV
Use mysqly::export_csv()
or mysqly::export_tsv()
to export data to CSV/TSV files:
|
|
|
Atomically toggle column value
Use mysqly::toggle()
to atomically update specific column value to opposite one from given pair:
|
|
|