Leaf DB Overview
Introduction
Leaf's "simple query builder" provides a convenient but usual way to quickly create and run database queries. It can be used to perform most database operations in your app.
Leaf's "simple query builder" currently supports Mysqli and PDO connections, though we still recommend using Mysqli. There's no need to worry about SQL injection as parameter binding is also supported and easy to use😉💪
Init
Initialising Leaf DB
Leaf DB has 2 different packages, 1 for mysqli and the other for PDO. So you can import which ever package you wish to use. Leaf recommends using the mysqli package.
Both DB:PDO and DB:Mysqli use the same methods, so all the code below works the same for whichever you're using. We'll alert you if something works differently.
DB connection
The first thing you need to do to use Leaf DB is to connect to your database. This can be achieved with connect()
Queries
Making simple queries
Queries with with Leaf DB are much like what you're used to. Though a query builder, we wan't to maintain the flexibility of normal database queries,
hence, we provided the query()
method to make your normal database queries.
As normal as this seems, we take it a step further by providing you with a much simpler way to use prepared statements.
We've looked at making queries, but then query()
still makes you type out whatever query you need
to use. It's certainly easier than raw queries, but it's nothing impressive. Below are some of Leaf DB's handy methods to make
queries even easier.💪😉
Retrieving Data
If you're attempting to use this, you've probably seen or used SELECT statements before. Leaf DB has provided an even easier way to use select.
Getting all rows from a table
To do this, we use the select()
method we saw above. All that we have to do is to pass in the table we
want to retrieve. For example, to get all users from the "users" table, we simply do:
To actually get the results, we'll have to chain fetchAll()
to the select method. fetchAll()
does the same thing that mysqli_fetch_all()
does to an mysqli result
To add a limit to the query, just use:
Getting a column from a table
Getting a single column, eg: getting all usernames from the users table
This is like saying SELECT username FROM users
. You can also pass in multiple options
You can get all columns with:
Getting a particular row from a table
Getting a particular row, eg: getting the user with the id of 1 from the users table. You acan achieve this with:
fetchObj
does the same thing as mysqli_fetch_obj
and fetch(PDO::FETCH_OBJ)
If you don't need the whole row, you can use:
Inserting Data
This operation uses INSERT. With Leaf DB:
Inserting Data
We use Leaf DB's insert method which takes in a "table" to insert data, a "column" and it's "value"
You can also add multiple columns like so:
Updating Data
This operation uses UPDATE. With Leaf DB:
Update
We use Leaf DB's update method which takes in a "table", a "column-value" to update and "conditions".
This will look for a post with the title of "Post One" and change it to "Post 1".
You can also have multiple options:
Deleting Data
This operation uses DELETE. With Leaf DB:
Delete
We use Leaf DB's delete method which takes in a "table", and "conditions".
This will look for a post with the title of "Post 1" and delete it.
You can also have multiple conditions:
Others
Row Count
Get the number of rows from select
Connection Close
Close the connection
Next Steps
Re-routing to index.phpSimple Routing
Request
Response