22 August 2013

Almost in every project I need a database class behind my BLL classes. I keep my SQL queries in BLL classes and use the database class to run the queries. This is kind of engine used by all classes in common.
I will explain how to implement and how to use step by step in comments code.

Note that this class does not use the deprecated MySQL library, it uses MySQLi library.
Here is the complete PHP Database Class source code;

First php is the configuration file for use in database class.
I call it dbconfig.php

Most of us have to use the $_GET in PHP to get the desired content id and print the contents on page. But we cannot trust the incoming paremeter directly from the url.
We should check the parameter and validate it.

If it is set

Check if the parameter is set or not

if ( isset($_GET['id']) )
{
// ok it is set.
}

Now I also check if it is empty or not;

If it is not empty

if ( isset($_GET['id']) && !empty($_GET['id']) )
{
// ok it is set and not empty
}

If you know the parameter must be integer, than lets checkIf it is a valid number.