\Nano4\DBSimple

A simple, lightweight DB class.

Summary

Methods
Properties
Constants
__construct()
select()
insert()
update()
$db
$name
$server_id
No constants found
No protected methods found
$db_conf
N/A
No private methods found
No private properties found
N/A

Properties

$db

$db : 

The PDO database object.

Type

$name

$name : 

The database name, extracted from the DSN.

Type

$server_id

$server_id : 

The server id (optional)

Type

$db_conf

$db_conf : 

The database configuration, if 'keep_config' is true.

It's a protected property, so only this class can use it.

Type

Methods

__construct()

__construct(Mixed  $conf, Bool  $keep_config = false) 

Build our DB\Simple object.

Parameters

Mixed $conf

The database configuration.

Bool $keep_config

Save the database configuration. Default: false.

The $conf may be either a JSON string, a JSON filename, or an Array. The Array must contain at least a 'dsn' member. If the database uses a username and password, it must also contain 'user' and 'pass' members. If the database has a unique server_id, then it should be specified as the 'sid' member.

The database name will be extracted from the DSN either as the 'dbname' property, or as the SQLite filename.

If $keep_config is true, then the configuration is stored in the protected $db_conf property.

select()

select(Mixed  $table, Array  $opts = array()) 

Perform a SELECT query.

Parameters

Mixed $table

The database table(s) we are querying against.

Array $opts

(Optional) An associative array of options.

The $table may be either a String, or an Array. If it is an Array, the tables will be joined using a comma.

The $opts can contain any of the following optional parameters:

'where' The WHERE statement. Can be a string or associative array.

'data' If 'where' is a string, this contains the placeholder data. This is not used at all if 'where' is an associative array.

'cols' The columns to return. Defaults to '*'.

'order' The ORDER BY statement: e.g. "timestamp DESC, name ASC"

'limit' The LIMIT statement.

'offset' The OFFSET statement (only used with 'limit').

'single' If set to true, it explicitly sets LIMIT 1, and returns the row data rather than the statement object.

'fetch' Change the PDO Fetch Mode. Default: PDO::FETCH_ASSOC.

insert()

insert(String  $table, Array  $data) 

Create a new row.

Parameters

String $table

The table to insert the data into.

Array $data

An associative array of data we're inserting.

update()

update(String  $table, Mixed  $where, Array  $cdata, Array  $wdata = null) 

Save an existing row.

Parameters

String $table

The table to insert the data into.

Mixed $where

The WHERE statement.

Array $cdata

The columns we are updating.

Array $wdata

The WHERE placeholder data (see below.)

If $where is an Array, then it's a standalone WHERE clause, and the $wdata parameter is not needed (and will be ignored.)

If $where is a string, then $wdata must contain the placeholder data for the WHERE statement.