Download the PHP package davidecesarano/embryo-pdo without Composer
On this page you can find all versions of the php package davidecesarano/embryo-pdo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download davidecesarano/embryo-pdo
More information about davidecesarano/embryo-pdo
Files in davidecesarano/embryo-pdo
Package embryo-pdo
Short Description Embryo PDO is a PHP SQL query builder using PDO.
License MIT
Homepage https://github.com/davidecesarano/embryo-pdo
Informations about the package embryo-pdo
Embryo PDO
A quick and light PHP query builder using PDO.
Requirements
- PHP >= 7.1
Installation
Using Composer:
Usage
- Connection
- Retrieving results
- Retrieving a single row
- Forcing array
- Aggregates
- Where conditions
- Simple Where
- OR condition
- AND/OR closure
- BETWEEN condition
- IN condition
- IS NULL condition
- Raw Where
- Method Aliases
- Joins
- Insert
- Update
- Delete
- Ordering, grouping, limit and offset
- Raw Query
- Pagination
- Security
- Debugging
Connection
Create a multidimensional array with database parameters and pass it at the Database
object. Later, create connection with connection
method.
Retrieving results
You can create a simple query:
The table
method returns a fluent query builder instance for the given table. This would build the query below:
To get data from the select, we can is loop through the returned array of objects:
Retrieving a single row
If you just need to retrieve a single row from the database table, you may use the same (get
) method.
If you don't even need an entire row, you may extract one or more values from a record using the select
method.
Forcing array
If you want to force return array of objects, you can use all
method
Aggregates
The query builder also provides a variety of aggregate methods such as count
, max
, min
, avg
, and sum
.
Where conditions
Simple Where
You may use the where
method to add where clauses to the query.
The most basic call to where requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. Finally, the third argument is the value to evaluate against the column.
For convenience, if you want to verify that a column is equal to a given value, you may pass the value directly as the second argument to the where method:
You may use a variety of other operators when writing a where clause:
OR condition
You may chain where constraints together as well as add "or" clauses to the query.
AND/OR closure
If you need to group an "or" or "and" condition within parentheses, you may pass a Closure as the first argument to the method:
This would build the query below:
BETWEEN condition
The whereBetween
/ whereNotBetween
method verifies that a column's value is between / not between two values:
IN condition
The whereIn
/ whereNotIn
method verifies that a given column's value is contained / not contained within the given array:
IS NULL condition
The whereNull
/ whereNotNull
method verifies that the value of the given column is NULL
/ not NULL:
Raw Where
The rawWhere
method can be used to inject a raw where condition into your query. This method accept an array of bindings argument.
Method aliases
Below is a table with all the methods of the where conditions and their aliases.
Method | Alias |
---|---|
where() | and() andWhere() |
orWhere() | or() |
whereBetween() | andBetween() andWhereBetween() |
orWhereBetween() | orBetween() |
whereNotBetween() | andNotBetween() andWhereNotBetween() |
orWhereNotBetween() | orNotBetween() |
whereIn() | andIn() andWhereIn() |
orWhereIn() | orIn() |
whereNotIn() | andNotIn() andWhereNotIn() |
orWhereNotIn() | orNotIn() |
whereNull() | andNull() andWhereNull() whereIsNull() andIsNull() andWhereIsNull() |
orWhereNull() | orNull() orWhereIsNull() orIsNull() |
whereNotNull() | andNotNull() andWhereNotNull() whereIsNotNull() andIsNotNull() andWhereIsNotNull() |
orWhereNotNull() | orNotNull() orWhereIsNotNull() orIsNotNull() |
Joins
The query builder may also be used to write simple join statements with leftJoin
, rightJoin
, crossJoin
, innerJoin
or rawJoin
methods:
Insert
You can insert row/s in database with insert
method.
This will return the last inserted id. The insert method also accepts the exec()
method and it will return true on success or false on failure.
Update
You can update row/s with update
method.
Delete
You can delete row/s with delete
method.
Ordering, grouping, limit and offset
You may use the groupBy
method to group the query results.
The orderBy
method allows you to sort the result of the query by a given column:
You may use the limit
method to limit the number of results returned from the query:
To skip a given number of results in the query, you may use the limit
and offset
methods:
Raw Query
Sometimes you may need to use a raw expression in a query. To create a raw expression, you may use the query
method:
The values
method binds a value to a parameter. Binds a value to a corresponding named placeholder in the SQL statement that was used to prepare the statement.
Pagination
Pagination means displaying all your fetched results in multiple pages instead of showing them all on one page.
To change the page number use page
query param in URI (http://example.com/?page=1
).
We will have this result:
If you want retrieve specific fields from records you may use:
Security
Embryo PDO uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.
Debugging
You may use the debug
method for for to dumps the information contained by a prepared statement directly on the output.
This would build the output below:
If you want only to show query, you may print object:
This would build the output below: