Updated July 28, 2023
Introduction to PHP Database Connection
The database is one of the important components of any programming language. To deal with a dynamic project and data management, we need to have a database. PHP supports various kinds of database connections with it. MySQL is one of the most widely used relational databases, and it is mostly used with PHP as well. Considering the term database connection in PHP, MySQL itself have various way to make connections in an application to play with the database operations. After making the connection of PHP-MYSQL, we can do various things like – insertion of records, deletion of records, updating the records, etc. This article will see database connection using the PHP language in various ways, so keep reading to grab it properly.
How to Connect PHP Database?
Before making a connection, we should have details like – Hostname, Database User Name, Database Password, Port (if application), etc. Every programming language has its own unique way of making a connection with the databases and playing with that. Database in PHP, not that much of a big task as we see in a programming language like JAVA. There is a very simple couple of lines code to connect with the database.
In the PHP language, we can make database connection in a below-mentioned way:
1. MySQL
This will work with the MySQL database only. This extension follows the old traditional way of communicating with a database. Now, every coming PHP version has deprecated this approach.
2. MySQLi Extension
This will work with the MySQL database only, but this is an improved version of MySQL.
3. PDO
It works with various databases. Usually, we consider this as the best approach out of these three. This one is considered as an object-oriented way of communicating with the MySQL database. The moment we create a connection, it gives us the object to deal with the MySQL related.
Examples
Given below are the examples mentioned:
Example 1 – PHP MYSQL Connection using MYSQL
Code:
$servername = "localhost";
$username = "root";
$password = "";
$link = mysql_connect($servername, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}else{
echo "Database Connected successfully"; // in case of success
}
The connection can be made successfully in the lower version of PHP. But, if we use this code, it says Deprecated: mysql_connect(): The MySQL extension is deprecated and will be removed in the future: use mysqli or PDO instead.
That’s why we should avoid using this technique to make a database connection in PHP language to the MySQL database.
Example 2 – PHP MYSQL Connection Using MYSQLi
We can make the connection using the MYSQLi in two ways.
MYSQLi Object-Oriented.
Code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Database Connection Code
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // in case of error
}else{
echo "Database Connected successfully"; // in case of success
}
?>
Now, we have $conn, the database connection object. We can use this object for all the communication to the database.
Code:
// selecting database "test1"
mysqli_select_db($conn,"test1");
You can also pass the database as an argument at the time of connection establishment.
Code:
$conn = new mysqli($servername, $username, $password, $databaseName);
MYSQLi Function (procedural) Way
Code:
$servername = "localhost";
$username = "root";
$password = "";
// Database Connection Code
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error()); // in case of error
}else{
echo "Database Connected successfully"; // in case of success
}
We can also use the other operation like database connection and other as mentioned above.
Example 3 – PDO PHP Database Connection
Again this is an Object-Oriented way of database connection in PHP. We can use various types of databases with this approach.
Code:
$servername = "localhost";
$username = "root";
$password = "";
try {
// Database Connection Code
$conn = new PDO("mysql:host=$servername;dbname=test1", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// in case of success
echo "Connected successfully";
}
catch(PDOException $e)
{
// in case of error
echo "Connection failed: " . $e->getMessage();
}
Now the question is Should I Use MYSQLi or PDO?
These both are the object-oriented way of database connection using PHP. We can consider this as a present and the future way of the database connection. But choosing out of these two is all about what kind of need you have with your project. If there is a MySQL database only, you can go with MYSQLi. But the moment the possibility of the database changes, from MySQL to MySQL SERVER or any other vendor, the PDO will be the best option. These both approach support the prepared statement while writing queries to do database operations. A prepared statement is an approach we can protect our application or the database from the SQL injection attack.
Conclusion
So, what’s in your mind. After coming across all the 3 mentioned above type of database connection techniques, we came to the conclusion that PDO is the best approach to move ahead with. The idea behind putting this on top is that we can use this approach to the connection, not just the MySQL database but also the other database like- MySQL Server. We should avoid using mysql_connect() to make sure our code is durable and future-ready.
Recommended Articles
This is a guide to PHP Database Connection. Here we discuss how to connect PHP, PHP MYSQL connection using MYSQL, PHP MYSQL connection using MYSQLi, PDO PHP database connection, etc. You may also look at the following articles to learn more –