Editor PHP 2.0.1

Database

DataTables Database connection object.

Create a database connection which may then have queries performed upon it.

This is a database abstraction class that can be used on multiple different databases. As a result of this, it might not be suitable to perform complex queries through this interface or vendor specific queries, but everything required for basic database interaction is provided through the abstracted methods.

Table of Contents

__construct()  : mixed
Database instance constructor.
any()  : bool
Determine if there is any data in the table that matches the query condition
commit()  : self
Commit a database transaction.
count()  : mixed
Get a count from a table.
debug()  : bool|self
Get / set debug mode.
delete()  : mixed
Perform a delete query on a table.
insert()  : mixed
Insert data into a table.
push()  : mixed
Update or Insert data. When doing an insert, the where condition is added as a set field
query()  : mixed
Create a query object to build a database query.
quote()  : mixed
Quote a string for a quote. Note you should generally use a bind!
raw()  : mixed
Create a `Query` object that will execute a custom SQL query. This is similar to the `sql` method, but in this case you must call the `exec()` method of the returned `Query` object manually. This can be useful if you wish to bind parameters using the query `bind` method to ensure data is properly escaped.
resource()  : resource
Get the database resource connector. This is typically a PDO object.
rollback()  : self
Rollback the database state to the start of the transaction.
select()  : mixed
Select data from a table.
selectDistinct()  : mixed
Select distinct data from a table.
sql()  : mixed
Execute an raw SQL query - i.e. give the method your own SQL, rather than having the Database classes building it for you.
transaction()  : self
Start a new database transaction.
update()  : mixed
Update data.

Methods

__construct()

Database instance constructor.

public __construct(mixed $opts) : mixed

@param string[] $opts Array of connection parameters for the database:

  array(
      "user" => "", // User name
      "pass" => "", // Password
      "host" => "", // Host name
      "port" => "", // Port
      "db"   => "", // Database name
      "type" => ""  // Datable type: "Mysql", "Postgres" or "Sqlite"
  )
Parameters
$opts : mixed
Return values
mixed

any()

Determine if there is any data in the table that matches the query condition

public any(string|array<string|int, string> $table[, array<string|int, mixed> $where = null ]) : bool
Parameters
$table : string|array<string|int, string>

Table name(s) to act upon.

$where : array<string|int, mixed> = null

Where condition for what to select - see Query->where().

Return values
bool

Boolean flag - true if there were rows

commit()

Commit a database transaction.

public commit() : self

Use with Database->transaction() and Database->rollback().

Return values
self

count()

Get a count from a table.

public count(mixed $table[, string $field = "id" ][, mixed $where = null ]) : mixed

@param string|string[] $table Table name(s) to act upon.

Parameters
$table : mixed
$field : string = "id"

Primary key field name @param array $where Where condition for what to select - see Query->where(). @return Number

$where : mixed = null
Return values
mixed

debug()

Get / set debug mode.

public debug([mixed $set = null ]) : bool|self

@param boolean $_ Debug mode state. If not given, then used as a getter.

Parameters
$set : mixed = null
Return values
bool|self

Debug mode state if no parameter is given, or self if used as a setter.

delete()

Perform a delete query on a table.

public delete(string|array<string|int, string> $table[, mixed $where = null ]) : mixed

This is a short cut method that creates an update query and then uses the query('delete'), table, where and exec methods of the query.

Parameters
$table : string|array<string|int, string>

Table name(s) to act upon. @param array $where Where condition for what to delete - see Query->where(). @return Result

$where : mixed = null
Return values
mixed

insert()

Insert data into a table.

public insert(string|array<string|int, string> $table, mixed $set[, mixed $pkey = '' ]) : mixed

This is a short cut method that creates an update query and then uses the query('insert'), table, set and exec methods of the query.

Parameters
$table : string|array<string|int, string>

Table name(s) to act upon. @param array $set Field names and values to set - see Query->set(). @param array $pkey Primary key column names (this is an array for forwards compt, although only the first item in the array is actually used). This doesn't need to be set, but it must be if you want to use the Result->insertId() method. @return Result

$set : mixed
$pkey : mixed = ''
Return values
mixed

push()

Update or Insert data. When doing an insert, the where condition is added as a set field

public push(mixed $table, array<string|int, mixed> $set[, mixed $where = null ][, mixed $pkey = '' ]) : mixed

@param string|string[] $table Table name(s) to act upon.

Parameters
$table : mixed
$set : array<string|int, mixed>

Field names and values to set - see Query->set(). @param array $where Where condition for what to update - see Query->where(). @param array $pkey Primary key column names (this is an array for forwards compt, although only the first item in the array is actually used). This doesn't need to be set, but it must be if you want to use the Result->insertId() method. Only used if an insert is performed. @return Result

$where : mixed = null
$pkey : mixed = ''
Return values
mixed

query()

Create a query object to build a database query.

public query(mixed $type[, string|array<string|int, string> $table = null ]) : mixed

@param string $type Query type - select, insert, update or delete.

Parameters
$type : mixed
$table : string|array<string|int, string> = null

Table name(s) to act upon. @return Query

Return values
mixed

quote()

Quote a string for a quote. Note you should generally use a bind!

public quote(mixed $val[, string $type = PDO::PARAM_STR ]) : mixed

@param string $val Value to quote

Parameters
$val : mixed
$type : string = PDO::PARAM_STR

Value type @return string

Return values
mixed

raw()

Create a `Query` object that will execute a custom SQL query. This is similar to the `sql` method, but in this case you must call the `exec()` method of the returned `Query` object manually. This can be useful if you wish to bind parameters using the query `bind` method to ensure data is properly escaped.

public raw() : mixed

@return Result

Tags
example

escape user input

$db
  ->raw()
  ->bind( ':date', $_POST['date'] )
  ->exec( 'SELECT * FROM staff where date < :date' );
Return values
mixed

resource()

Get the database resource connector. This is typically a PDO object.

public resource() : resource
Return values
resource

PDO connection resource (driver dependent)

rollback()

Rollback the database state to the start of the transaction.

public rollback() : self

Use with Database->transaction() and Database->commit().

Return values
self

select()

Select data from a table.

public select(string|array<string|int, string> $table[, mixed $field = "*" ][, mixed $where = null ][, mixed $orderBy = null ]) : mixed

This is a short cut method that creates an update query and then uses the query('select'), table, get, where and exec methods of the query.

Parameters
$table : string|array<string|int, string>

Table name(s) to act upon. @param array $field Fields to get from the table(s) - see Query->get(). @param array $where Where condition for what to select - see Query->where(). @param array $orderBy Order condition - see Query->order(). @return Result

$field : mixed = "*"
$where : mixed = null
$orderBy : mixed = null
Return values
mixed

selectDistinct()

Select distinct data from a table.

public selectDistinct(string|array<string|int, string> $table[, mixed $field = "*" ][, mixed $where = null ][, mixed $orderBy = null ]) : mixed

This is a short cut method that creates an update query and then uses the query('select'), distinct ,table, get, where and exec methods of the query.

Parameters
$table : string|array<string|int, string>

Table name(s) to act upon. @param array $field Fields to get from the table(s) - see Query->get(). @param array $where Where condition for what to select - see Query->where(). @param array $orderBy Order condition - see Query->order(). @return Result

$field : mixed = "*"
$where : mixed = null
$orderBy : mixed = null
Return values
mixed

sql()

Execute an raw SQL query - i.e. give the method your own SQL, rather than having the Database classes building it for you.

public sql(string $sql) : mixed

This method will execute the given SQL immediately. Use the raw() method if you need the ability to add bound parameters.

Parameters
$sql : string

SQL string to execute (only if _type is 'raw'). @return Result

@example Basic select

$result = $db->sql( 'SELECT * FROM myTable;' );

@example Set the character set of the connection

$db->sql("SET character_set_client=utf8");
$db->sql("SET character_set_connection=utf8");
$db->sql("SET character_set_results=utf8");
Return values
mixed

transaction()

Start a new database transaction.

public transaction() : self

Use with Database->commit() and Database->rollback().

Return values
self

update()

Update data.

public update(string|array<string|int, string> $table[, mixed $set = null ][, mixed $where = null ]) : mixed

This is a short cut method that creates an update query and then uses the query('update'), table, set, where and exec methods of the query.

Parameters
$table : string|array<string|int, string>

Table name(s) to act upon. @param array $set Field names and values to set - see Query->set(). @param array $where Where condition for what to update - see Query->where(). @return Result

$set : mixed = null
$where : mixed = null
Return values
mixed

Search results