Programming MySQL How to connect MySQL database in PHP – PHP MySQL

How to connect MySQL database in PHP – PHP MySQL

Before learning how to connect MySQL database in PHP, I would like to give a brief introduction about MySQL.

What is MySQL?

MySQL is an open source relational database management system (RDBMS) based on Structured Query Language (SQL). MySQL runs on all the platforms, including Linux, UNIX, windows etc. Although it can be used in a wide variety of applications, MySQL is often associated with web applications and online publishing.

MySQL is an important component of an open source enterprise pack known as LAMP. LAMP is a web development platform, actually a combination, which uses Linux as Operating system, Apache as web server, and MySQL as the database management system and PHP as scripting language.

*Before you start with the connection code it is recommended to for you to have introductory knowledge of PHP. If you know SQL, It’s a cherry on the cake.

How to connect MySQL Database in PHP?

PHP is flexible enough as we can use any database however most of the time developers use MySQL Database to store data while developing application in PHP. There are two ways to connect database with PHP.

  1. MySQli
  2. PDO (PHP Data Object)

Now a question arises, which method is to be used? The answer is very clear that it depends solely on your choice. The difference is that PDO can be used with all other supported databases and MySQLi works with MySQL database only.

*Here we are using MySQLi.

PHP MySQL Connection Example:

Connecting MySQL database is not at all a rocket science. Follow a few steps correctly to connect MySQL to PHP, as explained in the example below.

As a good practice you should store the username, password and database in variables before start of connection script. In that way you can change them at any them whenever required.

Now we start connection script to connect PHP to MySQL database, which can be achieved by mysqli_connect() function or using Object.

Before starting connection, you have to change the webhost, username, password and database_name according to your database webhost, database username, and database password and database name respectively.

Below I am showing a sample code for localhost connection of MySQL Database in PHP with both procedural and object oriented methods.

Connect MySQL Database by MySQLi procedural method

Connect MySQL Database by MySQLi Object oriented method

To use a database we need to select the database that we want to connect after the successful connection to MySQL database.

Database selection example in MySQL

Select MySQL database by MySQLi procedural method

Select MySQL database by MySQLi Object oriented method

After execution of database queries, at the end of script, you should close the database connection. To do so we have again procedural and Object oriented methods, illustrated below.

Close MySQLi connection by procedural method

Close MySQLi connection by Object oriented method

Complete codes using both procedural and object oriented methods are as following:

MySQLi connection process by Object oriented method

MySQLi connection process by procedural method

Download the code for procedural method OR Object oriented Method.

Leave a Reply