JavaScript Linux PHP All
Up

Search



Connecting To Database In PHP






Recent




PHP MySQL Pagination

PHP MySQL pagination is used to partition the data from the database to navigate the webpage easier and faster....


How To Use VPS Servers

You will learn how to use VPS servers correctly via the commands without the need of any interface or control panel....


Get Your Own Virtual Server Hosting

Virtual servers are widely used to host websites due to the great features they provide and the cheap costs....


Detailed Example Of A JavaScript Object Oriented Code

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....


Create A JavaScript Object Oriented Code

By having a JavaScript object oriented code, your program will be more clear and well structured....


PHP Database Connection


PHP Database Connection
1345   php

By having a PHP database connection for your program, it will be easy to append and retrieve records and save the statuses of your program.




What Is The Database?

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.



Database In Our Trip Example

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.



Let's Start   : )

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:


<?php
// Class definition
class Database{}
?>

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.


<?php
// Class definition
class Database{
// The constructor function
public function __construct(){


}

}
?>


The Class Properties

Inside the constructor, we will define our constant properties that we will use to connect to the database. We will need the below:


1.   The Database Host

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.


2.   The Database Username

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.


3.   The Database Password

It is the key of the room which is representing our database system.


4.   The Database Name

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:


<?php
// Class definition
class Database{
// The constructor function
public function __construct(){
$this->host = "your_DB_host_address";
$this->login = "your_DB_login_name";
$this->password = "your_DB_password";
$this->name = "your_DB_name";
}

}
?>

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.



The Class Methods

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:

A.   Connect Method


B.   Select Method


The methods are represented in the code as below:


<?php
// Class definition
class Database{
// The connect function
private function connect(){
$this->connection = mysql_connect($this->host, $this->login, $this->password) or die("Sorry! Cannot connect to the database");
}

// The select function
private function select(){
mysql_select_db($this->name, $this->connection);
}

}
?>

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.


The Complete Class Code

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.


<?php
// Class definition
class Database{
// The constructor function
public function __construct(){
// The properties
$this->host = "your_DB_host_address";
$this->login = "your_DB_login_name";
$this->password = "your_DB_password";
$this->name = "your_DB_name";

// The methods
$this->connect();
$this->select();
}
// The connect function
private function connect(){
$this->connection = mysql_connect($this->host, $this->login, $this->password) or die("Sorry! Cannot connect to the database");
}
// The select function
private function select(){
mysql_select_db($this->name, $this->connection);
}

}
?>


Call Your Class

To call and execute the Database class, we just need to create an instance for the class as below:


<?php
$database_class_instance = new Database();
?>

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.













Home · About · Contact · Terms · Company Website

· · · ·