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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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:
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.
This will work with the MySQL database only, but this is an improved version of MySQL.
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.
Given below are the examples mentioned:
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.
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.
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(); }
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.
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.
The above is the detailed content of PHP Database Connection. For more information, please follow other related articles on the PHP Chinese website!