PHP MySQL pagination is used to partition the data from the database to navigate the webpage easier and faster....
You will learn how to use VPS servers correctly via the commands without the need of any interface or control panel....
Virtual servers are widely used to host websites due to the great features they provide and the cheap costs....
Learn how to use the properties and methods in the object oriented code and how to call the methods from inside and outside the mother object....
The database is just like a storing room in your house where you store and retrieve your stuff according to your needs. For example, if you want to go for a trip, then you will need to go to the storing room to get your luggage, blankets, food/water equipments, etc. And when you return back from your trip, then you will need to return back the stuff you retrieved from the storing room and sort everything there.
In this article, the trip will be similar to the PHP program (or the PHP script), and the storing room will be similar to the database that will be connected to the PHP program. We will use the MySQL database which is famous and compatible with the PHP.
We'd love to keep the code neat, well organized and reuseable. That's why, we will use the OOP (Object Oriented Programming) for our code. We will create a PHP class called Database that will contain the required properties and functions to have a complete php database connection. It will be defined as below:
Now, we will add the constructor function to the class as below. The constructor function will be executed once we define an instance of the Database class.
Inside the constructor, we will define our constant properties that we will use to connect to the database. We will need the below:
It is where the database system is installed, maybe I am connecting to a database that is installed on a local server at my home, or I am connecting to a database that is installed on a remote server in a different country like USA, India, UK, Germany, etc. When we connect to the local server, then we call that connection a localhost connection.
The username of the database system is just like the person who is authorized to access a certain room via a private key he/she has. Other people can't access the room (the database) because they are not authorized, i.e. they don't have the private key.
It is the key of the room which is representing our database system.
The database name is similar to the door number, each room has a specific number, so when you want to access a certain room, you need to know its number first. When you start writing codes for more than one project, you will need to create more than one database instance and you need to give every instance a special name to be able to connect each project to its corresponding database.
Let us now see how the four properties are respresented in the PHP code:
Notice that if you are installing the database system on your local machine, then the database host will be localhost, and if you are connecting to the database which is on a remote server, then the database host can be the IP address of the remote server or the hostname which resolves the IP address.
When you want to get something from a cupboard inside your room, you need first to access the room and then select the right cupboard which contains the thing you are looking for. In our example, accessing the room is similar to connecting to the database whole system and selecting the right cupboard is similar to selecting the database instance within the full database system. We have two methods:
The methods are represented in the code as below:
In the code above, the two methods connect & select are calling the MySQL built-in functions mysql_connect & mysql_select_db respectively to establish the connection and then to connect to the desired database instance. Both methods we have created will be called from the constructor when creating an instance for the Database class.
After adding the properties to the methods, our PHP database connection will look like below. Notice that we are calling the methods from the constructor.
To call and execute the Database class, we just need to create an instance for the class as below:
When you create the instance of the Database class, the constructor will be called and then the properties will be defined and the methods will be called to connect to the database general system and to select the desired database.
Our PHP database connection code is complete now and it is fully object oriented. You can include the database class only once in an external file and call it from any page you want whenever needed.