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.
- MySQli
- 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.
$db_username = "username"; $db_password = "password"; $db_name = "database_name"; $my_host = "webhost";
Now we start connection script to connect PHP to MySQL database, which can be achieved by mysqli_connect() function or using Object.
$connection = new mysqli('localhost',$db_username, $db_password, $db_name);
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
<?php $db_username = "root"; $db_password = ""; $db_name = "test_db"; $my_host = "localhost"; #Create connection to database $connection = new mysqli($my_host,$db_username, $db_password, $db_name); if(!$connection) { die("Database connection Failed"); } echo "Database connected successfully"; ?>
Connect MySQL Database by MySQLi Object oriented method
<?php $db_username = "root"; $db_password = ""; $db_name = "test_db"; $my_host = "localhost"; #Create connection to database $connection = new mysqli($my_host,$db_username, $db_password, $db_name); if($connection) { echo "Database connected successfully"; } else { echo "Database connection Failed"; } ?>
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
mysqli_select_db($connection, $db_name) or die('Unable to select the database');
Select MySQL database by MySQLi Object oriented method
$connection->select_db($db_name) or die('Unable to select the database');
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
mysqli_close($connection);
Close MySQLi connection by Object oriented method
$connection->close();
Complete codes using both procedural and object oriented methods are as following:
MySQLi connection process by Object oriented method
<?php $db_username = "root"; $db_password = ""; $db_name = "test_db"; $my_host = "localhost"; $connection = new mysqli($my_host,$db_username, $db_password); if($connection){ $connection->select_db($db_name) or die('Unable to select the database'); echo "Database connected Successfully"; }else{ echo "Database connection Failed"; } $connection->close(); ?>
MySQLi connection process by procedural method
<?php $db_username = "root"; $db_password = ""; $db_name = "test_db"; $my_host = "localhost"; $connection = mysqli_connect($my_host, $db_username, $db_password); if($connection){ mysqli_select_db($connection, $db_name) or die('Unable to select the database'); echo "Database connected successfully"; }else{ echo "Database connection Failed"; } mysqli_close($connection); ?>
Download the code for procedural method OR Object oriented Method.