Menu

## Database
in package

Database.php : 32

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.
type() : mixed
  Get the database type (e.g. Postgres, Mysql, etc)
update() : mixed
  Update data.

### Methods

#### __construct()

Database.php : 51

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

Database.php : 98

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 : 117

Commit a database transaction.

`public commit() : self`

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

##### Return values

self —

#### count()

Database.php : 132

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

Database.php : 152

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

Database.php : 178

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

Database.php : 201

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

Database.php : 225

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

Database.php : 257

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

Database.php : 269

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

Database.php : 293

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

  Safely escape user input

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

##### Return values

mixed —

#### resource()

Database.php : 303

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

`public resource() : resource`

##### Return values

resource —

PDO connection resource (driver dependent)

#### rollback()

Database.php : 315

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 : 336

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

Database.php : 362

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

Database.php : 397

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

Database.php : 410

Start a new database transaction.

`public transaction() : self`

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

##### Return values

self —

#### type()

Database.php : 420

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

`public type() : mixed`

##### Return values

mixed —

#### update()

Database.php : 438

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