Download the PHP package delight-im/db without Composer
On this page you can find all versions of the php package delight-im/db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download delight-im/db
More information about delight-im/db
Files in delight-im/db
Package db
Short Description Safe and convenient SQL database access in a driver-agnostic way
License MIT
Homepage https://github.com/delight-im/PHP-DB
Informations about the package db
PHP-DB
Safe and convenient SQL database access in a driver-agnostic way
Features
- Convenient interface
- Write less code for the same tasks
- Safe against SQL injections through the use of prepared statements
- Data returned by the server has the correct native types (i.e. no stringified fields)
- Driver-agnostic support for multiple database systems
Requirements
- PHP 5.6.0+
- PDO extension
Installation
-
Include the library via Composer [?]:
- Include the Composer autoloader:
Usage
Connecting to a database
-
From a dynamic database configuration:
This is the most convenient way to establish the database connection since the correct DSN will be created for you automatically. You can use the various setters with a fluent interface in order to set up your configuration.
You can leave out any options that you don't need and check the
PdoDataSource
class for other available setters.Connections will be created lazily, i.e. they won't consume any resources before they're actually used. All that happens automatically.
-
From a DSN (data source name):
The parameters of the
PdoDsn
constructor are exactly the same as the parameters to thePDO
constructor. But if you don't need anotherPDO
instance, it's more efficient to go withoutPDO
and create this library's instance from a DSN directly.Connections will be created lazily, i.e. they won't consume any resources before they're actually used.
-
Using an existing
PDO
instance:This will not cause another database connection to be created but instead re-use the existing connection from the
PDO
instance that you provided.Furthermore, you can benefit from the strong guarantees about this library's behavior and from its convenience while still being able to use your existing
PDO
instance without any side effects.Just pass
true
as the second argument to thefromPdo
method to completely preserve the original state of yourPDO
instance:
Available drivers for database systems
Selecting data
When selecting data, you get the desired results with just one method call. And, of course, you can easily choose whether you want to get multiple rows, a single row, a single value only, or a single column from all rows.
Inserting data
For simple insertions, you can use a convenient shorthand:
Does your table have automatically generated primary IDs? Access to these inserted IDs is available with another short method call. The sequence name is only required for some database drivers, e.g. PostgreSQL.
If you need to execute more advanced statements, please refer to section "Executing statements" below.
Updating data
For simple updates, you can use a convenient shorthand as well:
Do you want to know how many rows have been updated by this operation? Just grab the return value from the update
method call.
If you need to execute more advanced statements, please refer to section "Executing statements" below.
Deleting data
Again, for simple deletions, you can use a convenient shorthand:
The return value from the delete
method call will tell you how many rows have been deleted by this operation.
If you need to execute more advanced statements, please refer to section "Executing statements" below.
Executing statements
You can execute any arbitrary SQL statements as shown in the following examples:
For every statement that you execute, the return value will be the number of rows affected.
If the statement that you're executing is an INSERT
, you can get the inserted IDs via the getLastInsertId
method, again.
Transactions
Transaction controls are as easy as they should be:
Error handling
The methods from this library do not return any error codes. You will be informed about any problem via an exception.
Input escaping
There's no need for you to escape any input manually. Just use the methods as shown above by using placeholders and passing the data separately. All input will be escaped for you automatically.
Performance profiling
In order to monitor query performance during development, you can enable performance profiling:
Whenever you want to see the analyzed queries or store them in a log file, you can get all measurements recorded by the profiler as an array:
Typically, you'll want to sort the measurements before retrieving them, so that the longest-running queries are listed first:
Server and client information
In order to retrieve some information about the database server that you're connected to or about the database client used by PHP, you can use one of the following methods:
Listeners
Connection established
Contributing
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
License
This project is licensed under the terms of the MIT License.
All versions of db with dependencies
ext-pdo Version *