Download the PHP package chillerlan/php-database without Composer

On this page you can find all versions of the php package chillerlan/php-database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-database

chillerlan/php-database

A PHP 7.4+ SQL client and querybuilder for the most common databases.

PHP Version Support version license Coverage Scrunitizer Packagist downloads
Continuous Integration

Documentation

Requirements

Installation

requires composer

composer.json

(note: replace dev-main with a version boundary)

Profit!

Usage

Getting started

Both, the DriverInterface and QueryBuilder can be instanced on their own. However, since the QueryBuilder requires an instance of DriverInterface it's recommended to just use Database which instances both and provides all of their methods. A DriverInterface requires a DatabaseOptions object and accepts a Psr\SimpleCache\CacheInterface and a Psr\Log\LoggerInterface as optional parameters, the QueryBuilder accepts a LoggerInterface as additional parameter.

which is equivalent to

now instance a driver with these options

via the querybuilder

recommended way via Database, which provides all methods of DriverInterface and QueryBuilder

Properties of DatabaseOptions

property type default allowed description
$driver string null DriverInterface database driver to use (FQCN)
$querybuilder string null QueryBuilderInterface query builder to use (FQCN) [optional]
$host string 'localhost'
$port int null
$socket string null
$database string null
$username string null
$password string null
$use_ssl bool false indicates whether the connection should use SSL or not
$ssl_key string null
$ssl_cert string null
$ssl_ca string null
$ssl_capath string null
$ssl_cipher string null
$mysqli_timeout int 3
$mysql_charset string 'utf8mb4' How to support full Unicode in MySQL
$pgsql_charset string 'UTF8'
$odbc_driver string null
$convert_encoding_src string null supported encodings mb_convert_encoding(), used in Result
$convert_encoding_dest string 'UTF-8' supported encodings mb_convert_encoding(), used in Result
$mssql_timeout int 3
$mssql_charset string 'UTF-8'
$mssql_encrypt bool false

Methods of DriverInterface

method return
__construct(DatabaseOptions $options, CacheInterface $cache = null) -
connect() DriverInterface
disconnect() bool
getDBResource() resource|object
getClientInfo() string
getServerInfo() string
escape($data) string (subject to change)
raw(string $sql, string $index = null, bool $assoc = true) Result|bool
rawCached(string $sql, string $index = null, bool $assoc = true, int $ttl = null) Result|bool
prepared(string $sql, array $values = [], string $index = null, bool $assoc = true) Result|bool
preparedCached(string $sql, array $values = [], string $index = null, bool $assoc = true, int $ttl = null) Result|bool
multi(string $sql, array $values) bool (subject to change)
multiCallback(string $sql, array $data, $callback) bool (subject to change)

Methods of QueryBuilder

All methods of QueryBuilder are also accessible as properties via magic methods. The returned object is a Statement of \chillerlan\Database\Query\* interfaces.

method return
__construct(DriverInterface $db, LoggerInterface $logger = null) -
select() Select
insert() Insert
update() Update
delete() Delete
create() Create
alter() Alter
drop() Drop

Methods of Database

in addition to DriverInterface and QueryBuilderInterface methods

method return
__construct(DatabaseOptions $options, CacheInterface $cache = null, LoggerInterface $logger = null) -
getDriver() DriverInterface
getQueryBuilderFQCN() QueryBuilderInterface|null

The Statement interface

method return description
sql() string returns the SQL for the current statement
bindValues() array returns the values for each '?' parameter in the SQL
query(string $index = null Result Executes the current statement. $index is being used in "SELECT" statements to determine a column to index the Result by. $values.
multi(array $values = null) bool Executes the current statement as multi query. $values needs to be a multi dimensional array with each row.
callback(array $values = null, $callback = null) bool Executes the current statement. $index is being used in "SELECT" statements to determine a column to index the Result by. $values and $callback can be used to provide multiple values on multi row "INSERT" or "UPDATE" queries.

Create

method return
database(string $dbname = null) CreateDatabase
table(string $tablename = null) CreateTable

CreateDatabase

method description
ifNotExists()
name(string $dbname = null)
charset(string $collation)

CreateTable

method description
ifNotExists()
name(string $tablename = null)
charset(string $collation)
primaryKey(string $field)
field(string $name, string $type, $length = null, string $attribute = null, string $collation = null, bool $isNull = null, string $defaultType = null, $defaultValue = null, string $extra = null)
int(string $name, int $length = null, $defaultValue = null , bool $isNull = null, string $attribute = null) convenience shortcut for field(), also tinyint(...)
varchar(string $name, int $length, $defaultValue = null , bool $isNull = null)
decimal(string $name, string $length, $defaultValue = null , bool $isNull = null)
text(string $name, $defaultValue = null , bool $isNull = true) also tinytext()
enum(string $name, array $values, $defaultValue = null , bool $isNull = null) currently the only way to create an "ENUM" field

The generated Query will look something like this

Note that additional constraints and attributes will be appended regardless of the Query dialect

Insert

method description
into(string $table) The table where to insert data
values(array $values) An array of values where each row represents a row to insert [['column' => 'value', ...], ...]

As an alternative, you can provide the values via a callback

Select

method description
distinct() sets the "DISTINCT" statement (if the Query dialect supports it)
cols(array $expressions) An array of column expressions. If omitted, a SELECT * ... will be performed. Example: ['col', 'alias' => 'col', 'alias' => ['col', 'sql_function']]
from(array $expressions) An array of table expressions. Example: ['table', 'alias' => 'table']
groupBy(array $expressions) An array of expressions to group by.
where($val1, $val2, $operator = '=', $bind = true, $join = 'AND') Adds a "WHERE" clause, comparing $val1 and $val2 by $operator. $bind specifies whether the value should be bound to a '?' parameter (default) or not (no effect if $val2 is a Select interface). If more than one "WHERE" statement exists, they will be joined by $join.
openBracket($join = null) puts an opening bracket ( at the current position in the "WHERE" statement
closeBracket() puts a closing bracket ) at the current position in the "WHERE" statement
orderBy(array $expressions) An array of expressions to order by. ['col1', 'col2' => 'asc', 'col3' => 'desc']
offset(int $offset) Sets the offset to start from
limit(int $limit) Sets a row limit (page size)
count() Executes the statement to perform a SELECT COUNT(*) ... and returns the row count as int
cached() Performs a chached query

Update

method description
table(string $tablename) The table to update
set(array $set, bool $bind = true) $set is a key/value array to update the table with. $bind determines whether the values should be inserted into the Query (unsafe! use only for aliases) or be replaced by parameters (the default).
where($val1, $val2, $operator = '=', $bind = true, $join = 'AND') see Select::where()
openBracket($join = null) see Select::openBracket()
closeBracket() see Select::closeBracket()

Single row update

Update multiple rows

The generated SQL for both examples would look like the following, the difference is that one performs a single query, while the other loops through the given value array in the open prepared statement.

Delete

method description
from(string $table) The table to delete from (multi table not supported yet)
where($val1, $val2, $operator = '=', $bind = true, $join = 'AND') see Select::where()
openBracket($join = null) see Select::openBracket()
closeBracket() see Select::closeBracket()

The Result and ResultRow objects

Result implements \SeekableIterator, \ArrayAccess and \Countable, ResultRow extends it.

Result

property description
length

methods in addition to \SeekableIterator, \ArrayAccess and \Countable

method description
__construct($data = null, $sourceEncoding = null, $destEncoding = 'UTF-8') If $data is of type \Traversable, \stdClass or array, the Result will be filled with its values. If $sourceEncoding is present, the values will be converted to $destEncoding via mb_convert_encoding().
__merge(Result $result) merges one Result object into another (using array_merge())
chunk(int $size) splits the Result into chunks of $size and returns it as array (using array_chunk())

methods from Enumerable

method description
toArray() returns an array representation of the Result
map($callback) collects the result of $callback for each value of Result and returns it as array
each($callback) similar to map(), except it doesn't collect results and returns the Result instance
reverse() reverses the order of the Result (using array_reverse())

ResultRow

ResultRow allows to call the result fields as magic methods or properties. If called as method, you may supply a callable as argument which then takes the field value as argument. Fancy, huh?

Result and ResultRow examples

map() and each()

__merge(), toArray(), chunk() and reverse()


All versions of php-database with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2
chillerlan/php-settings-container Version ^1.0
chillerlan/php-traits Version ^3.0
psr/simple-cache Version ^1.0
psr/log Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package chillerlan/php-database contains the following files

Loading the files please wait ....