Download the PHP package paragonie/easydb without Composer
On this page you can find all versions of the php package paragonie/easydb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package easydb
EasyDB - Simple Database Abstraction Layer
PDO lacks brevity and simplicity; EasyDB makes separating data from instructions easy (and aesthetically pleasing).
EasyDB was created by Paragon Initiative Enterprises as part of our effort to encourage better application security practices.
Check out our other open source projects too.
If you're looking for a full-fledged query builder, check out Latitude and Aura.SqlQuery, which can be used with EasyDB.
If you'd like to use EasyDB but cache prepared statements in memory for multiple queries (i.e. to reduce database round-trips), check out our EasyDB-Cache wrapper class.
Installing EasyDB
First, get Composer, if you don't already use it.
Next, run the following command:
If you've installed Composer in /usr/bin
, you can replace
/path/to/your/local/composer.phar
with just composer
.
Why Use EasyDB? Because it's cleaner!
Let's refactor a dangerous PHP snippet that previously used string concatenation to pass user input
instead of prepared statements. For example, imagine something that just dropped {$_GET['blogpostid']}
into the
middle of a mysql_query()
statement. Let's make it secure.
The PDO Way
That's a little wordy for such a simple task. If we do this in multiple places, we end up repeating ourselves a lot.
The EasyDB Solution
We made it a one-liner.
What else can EasyDB do quickly?
Insert a row into a database table
This is equivalent to the following SQL query (assuming $_POST['blogpostid']
is equal to 123
, $_SESSION['user']
is equal to 234
, $_POST['body']
is
equal to test
, and $_POST['replyTo']
is equal to 3456
):
Build an insert without executing
Update a row from a database table
This is equivalent to the following SQL query
(assuming $_POST['comment']
is equal to 789
):
Delete a row from a database table
This is equivalent to the following SQL query:
Fetch a single row from a table
Note: This expects a variadic list of arguments, not an array. If you have multiple parameters, stack them like this:
This is wrong:
Fetch a single column from a single row from a table
Note: cell()
expects a variadic list of arguments, not an array. If you have
multiple parameters, stack them like this:
This is wrong:
Alternatively, you can use single()
instead of cell()
if you really
want to pass an array.
Try to perform a transaction
Generate dynamic query conditions
Note: Passing values with conditions is entirely optional but recommended.
Variable number of "IN" arguments
Grouping of conditions
Insert and Update with custom placeholder
Since Version 2.12.0, EasyDB supports placeholders for calling stored procedures and SQL functions when inserting or updating data.
The EasyPlaceholder
class is constructed in the same fashion as other EasyDB methods: The first
argument, the "mask", must be a string. The mask may contain ?
placeholders, and any subsequent
arguments will fill in for the ?
placeholders when the query is executed.
Security warning: Do not concatenate user input into the first parameter.
EasyPlaceholder
can be used in insert()
, insertIgnore()
, insertOnDuplicateKeyUpdate()
,
and update()
.
What if I need PDO for something specific?
Can I create an EasyDB wrapper for an existing PDO instance?
Yes! It's as simple as doing this:
How do I run tests ?
Using Psalm's Security Analysis with EasyDB
First, make sure you've read the Psalm documentation.
EasyDB's API exposes several taint sinks. Next, run the following command on your codebase that uses EasyDB to identify sources of SQL injection risk.
This will expose where you're passing tainted data to EasyDB in a potentially unsafe way.
Troubleshooting Common Issues
Only one-dimensional arrays are allowed
This comes up a lot when trying to pass an array of parameters to run()
.
EasyDB::run()
expects a query string, then any number of optional parameters.
It does NOT expect an array of all the parameters.
If you want to use an API that looks like $obj->method($string, $array)
,
use safeQuery()
instead of run()
.
Alternatively, you can flatten your array with the splat operator:
EasyDB's run()
method is a variadic wrapper for safeQuery()
, so either
solution is correct.
Support Contracts
If your company uses this library in their products or services, you may be interested in purchasing a support contract from Paragon Initiative Enterprises.