Menu

## Database
in package

Database.php : 30

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() : Number
  Get a count from a table.
debug() : bool|self
  Get / set debug mode.
delete() : Result
  Perform a delete query on a table.
insert() : Result
  Insert data into a table.
push() : Result
  Update or Insert data. When doing an insert, the where condition is added as a set field.
query() : Query
  Create a query object to build a database query.
quote() : string
  Quote a string for a quote. Note you should generally use a bind!
raw() : Result
  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() : PDO
  Get the database resource connector. This is typically a PDO object.
rollback() : self
  Rollback the database state to the start of the transaction.
select() : Result
  Select data from a table.
selectDistinct() : Result
  Select distinct data from a table.
sql() : Result
  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.
type() : mixed
  Get the database type (e.g. Postgres, Mysql, etc).
update() : Result
  Update data.

### Methods

#### __construct()

Database.php : 51

Database instance constructor.

`public __construct(array<string, string|\PDO> $opts) : mixed`

##### Parameters

$opts : array<string, string|\PDO>

  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"
      )

##### Return values

mixed —

#### any()

Database.php : 95

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()

Database.php : 114

Commit a database transaction.

`public commit() : self`

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

##### Return values

self —

#### count()

Database.php : 131

Get a count from a table.

`public count(string|array<string|int, string> $table[, string $field = 'id' ][, array<string|int, mixed> $where = null ]) : Number`

##### Parameters

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

  Table name(s) to act upon.

$field : string = 'id'

  Primary key field name

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

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

##### Return values

Number —

#### debug()

Database.php : 152

Get / set debug mode.

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

##### Parameters

$set : bool = null

  Debug mode state. If not given, then used as a getter.

##### Return values

bool|self —

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

#### delete()

Database.php : 177

Perform a delete query on a table.

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

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.

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

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

##### Return values

Result —

#### insert()

Database.php : 201

Insert data into a table.

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

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.

$set : array<string|int, mixed>

  Field names and values to set - see Query->set().

$pkey : array<string|int, mixed> = ''

  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 values

Result —

#### push()

Database.php : 226

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

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

##### Parameters

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

  Table name(s) to act upon.

$set : array<string|int, mixed>

  Field names and values to set - see Query->set().

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

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

$pkey : array<string|int, mixed> = ''

  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 values

Result —

#### query()

Database.php : 259

Create a query object to build a database query.

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

##### Parameters

$type : string

  Query type - select, insert, update or delete.

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

  Table name(s) to act upon.

##### Return values

Query —

#### quote()

Database.php : 272

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

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

##### Parameters

$val : string

  Value to quote

$type : string = PDO::PARAM_STR

  Value type

##### Return values

string —

#### raw()

Database.php : 295

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() : Result`

##### Return values

Result —

@example Safely escape user input

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

#### resource()

Database.php : 305

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

`public resource() : PDO`

##### Return values

PDO —

PDO connection resource (driver dependent)

#### rollback()

Database.php : 317

Rollback the database state to the start of the transaction.

`public rollback() : self`

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

##### Return values

self —

#### select()

Database.php : 340

Select data from a table.

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

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.

$field : array<string|int, mixed> = '*'

  Fields to get from the table(s) - see Query->get().

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

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

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

  Order condition - see Query->order().

##### Return values

Result —

#### selectDistinct()

Database.php : 367

Select distinct data from a table.

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

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.

$field : array<string|int, mixed> = '*'

  Fields to get from the table(s) - see Query->get().

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

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

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

  Order condition - see Query->order().

##### Return values

Result —

#### sql()

Database.php : 402

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) : Result`

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 values

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");

#### transaction()

Database.php : 415

Start a new database transaction.

`public transaction() : self`

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

##### Return values

self —

#### type()

Database.php : 425

Get the database type (e.g. Postgres, Mysql, etc).

`public type() : mixed`

##### Return values

mixed —

#### update()

Database.php : 444

Update data.

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

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.

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

  Field names and values to set - see Query->set().

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

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

##### Return values

Result —

## Search results