1. Code
  2. PHP

Simple PHP Class-Based Querying

Scroll to top
Read Time: 8 min

Though it is usually advisable to use some sort of framework or CMS, sometimes a project is small enough such that those options would weigh down the development. However, even in smaller projects, separating presentational elements from backend querying should not be ignored. This tutorial will walk you through creating a basic class-based querying engine for PHP and MySQL.

Step 1. Setup the Project

The first thing we are going to want to do is make some specific files and directories. Here's how I like to setup my projects. You can, of course, feel free to change the names and structure to your liking. Just make sure you change the require's later on as well.

Make directories

We'll need a new directory to hold everything. In this case, I called it tut. Inside of that, I put my configuration files in a directory called conf. Then, I will make an inc directory (short for include) and put a "class" directory inside of that.

Add Files

Then, inside /conf, we will make config.php. Inside /inc/class we will make DAL.php. Finally, in the root directory, we will make index.php.

DAL stands for "Data Access Layer" or "Data Access Link".

In multi-tiered architecture, it is essentially used to translate database query results into objects and vice-versa.

Step 2. Setup the Database

We need to make a database and populate it with some data. For the purposes of this tutorial, it will just be a two-table database with a single one-to-many relationship. This is just so we can show our querying engine spanning at least one relationship.

Create tables

So, in a database named "tut", let's make a table called makes and a table called models. The makes table will have fields "id" and "name" and the models table will have fields "id","make", and "name".

Add some data

Now we can just add some makes (like Ford, Chevy, etc.) as data in the makes table and some models that those manufacturers are responsible for.

This tutorial assumes you have some working knowledge of databases and SQL, so I won't go into details about the relation/foreign key setup.

Step 3. The Database Connection

Usually, I don't like working with raw constants in PHP. I will typically define a bunch of things then make some functions to hook into those constants. For this example, let's just keep things simple and use the constants.

Define connection variables

In our /conf/config.php file, let's setup our database connection variables. While we are at it, let's throw an include to our DAL.php script.

This setup assumes you are running MySQL on it's default port.

Create connection function

Now, inside /inc/class/DAL.php, we will make a function that we will use to connect to our database.

The connection, as well as all forthcoming queries, will live inside a class named DAL. Wrapping all database involvement inside a single class allows us to manipulate our queries later without needing to touch business or presentation layer scripts. Also, it provides some degree of mock namespacing.

In the case of this class, we will add a constructor even though it doesn't need to do anything.

Notice that the scope of the dbconnect method is private. This is because we should not need to connect to the database from outside our DAL. Instead, we will have public query methods which will call the dbconnect from inside the DAL. A little confusing? No worries, read on.

Step 4. Create Generic Query Tools

To abstract our queries so that we can reuse short pieces of code, we will need two things. First, we will need some sort of "generic query result" class. Second, we will need a generic querying method inside our DAL.

Create generic query result class

The purpose of all of this is to be able to convert SQL queries into objects and minimize use of the ugly while($row = mysql_fetch_array($result)) loop. Objects are far easier to work with and allow us to use properties instead of array keys.

In short, we want to make a class that will create property names on the fly and store data associated with those properties.

We will put this class inside our /inc/class/DAL.php script. Since it is a new class, it will be outside the DAL class.

Create generic query method

Now, inside our DAL class, we need to make a generic querying method that will turn SELECT queries into DALQueryResult objects.

Basically, we want to turn each returned field name into a property of the DALQueryResult object.

Here is a private function that accepts a SQL query. It connects to the database and runs the query. Then, it checks to see if there are any results. If there are not any results, it returns null on a SELECT query, false on other queries. If the query was successful and the query was not a SELECT query, it will return true. If it was a SELECT, then it converts the results into an array of DALQueryResult objects. This mimics the results that one would normally get from a mysql_query.

Step 5. Write a Specific Query

Now we are ready to actually write a SQL query. DAL queries should be very specific both in name and purpose. Let's make one that finds all models of a given make.

This will be our first public method.

Here we are just writing the query and returning the result in the form of DALQueryResult objects. Our generic query method takes care of the itterations and decision making.

Finished DAL

At this point, our DAL.php script is finished. It should look like the following.

Step 6. Use the DAL

Now, let's finally head over to our /index.php script and display our results using the DAL. All we need to do is include our /conf/config.php file, instantiate the DAL, and do something with the data. Here's an example.

As you can see, we now have results that we can call the field names as properties of a PHP object.

Step 7. Taking Things one Step Further

Often, it will be useful to convert the generic DALQueryResult into a more specific object. In this case, you can write business objects that accept a DALQueryResult as a constructor parameter. Then, you just use that to build the new object.

Here's an example

Then, just write a query to return an array of these objects instead of an array of generic DALQueryResult objects.

Remember, always name your queries very specifically.

Building specific objects can become very useful when calculations are needed to extract meaningful data from fields.

Hope you all enjoyed the tutorial! Good luck.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.