Download the PHP package elbucho/database without Composer
On this page you can find all versions of the php package elbucho/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package database
elbucho/database
This project provides a utility wrapper around PDO requests for MySQL. It allows you to lazy-load multiple different database connections in one callable object, and gives you a streamlined access to PDO's methods.
Configuration
The Database class is instantiated by passing it an Elbucho\Config object with one or more DSNs in it. See the elbucho/config documentation for instructions on how to place and read in your config files.
DSNs
Database connections must be stored in the "dsns" key of your Config object, and each must have the below required keys. You can include multiple connections in one Config object.
The required keys for each database connection are:
- host => This is the hostname (eg. localhost)
- dbname => The name of the database you're connecting to
- user => Your database connection's username
- pass => Your database connection's password
Additionally, the optional key "port" can be specified with the port number your database server is running on. This defaults to 3306.
Default Handle
If your config file only contains information for one database connection, the Database object will set the handle for this connection to "default". If you have multiple connections, each connection must be prefaced with the handle name for that connection.
You can override the default handle name by specifying a 'default_handle' key in your config:
Below are two sample .yml files. This first one shows a singular connection, and it will be automatically assigned the default handle.
This .yml file shows multiple connections (eg. a dev and production db server):
This will create two handles in the $database object: "dev" and "prod".
Querying the Database
Once your config file is set, you can instantiate a database object and query it in one of two ways:
$database->query():
This method takes up to 3 arguments: your query itself, any parameters you wish to pass to the MySQL engine, and the handle to query (defaults to "default"). It returns an array of results or throws a \PDOException if there was an issue with the query.
$database->exec():
This method takes the same 3 arguments as query(), but does not return results. It is useful for passing commands to MySQL that you do not require a return for:
Fetching last insert ID
If you've just inserted a row or rows into your database, you can get the last insert id by typing:
Again, if the handle is left out, it will use whatever your default handle is set to.
Fetching the number of rows affected by the previous query
If you've just updated / inserted / deleted one or more rows in your database, you can get the count of rows affected by using:
Similar to getLastInsertId()
, if you leave out the handle, it will use whatever your default
handle is set to.
Setting Attributes
You can set PDO attributes on a given connection via the setAttribute method. This works identically to the PDO setAttribute method, with the exception that a handle is also passed:
All versions of database with dependencies
doctrine/inflector Version ^1.0
pds/skeleton Version ^1.0
elbucho/config Version *
ext-pdo Version *