PHP MySQL Database Connectivity
PHP MySQL Database Connectivity
$servername = "localhost";
$username = "root";
$password = "";
// Check connection
if (!$conn) {
error
}else{
}
2 Ways to Connect to MySQL database using PHP
There are two popular ways to connect to a MySQL database using PHP:
The guide also includes explanations for the credentials used in the PHP scripts and potential errors you may come across using MySQLi and PDO.
The PHP script for connecting to a MySQL database using the MySQLi procedural approach is the following:
<?php
$servername = "localhost";
$database = "database";
$username = "username";
$password = "password";
// Create connection
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_close($conn);
?>
Credentials Explained
The first part of the script is four variables (server name, database, username, and password) and their respective values. These values should
correspond to your connection details.
Next is the main PHP function mysqli_connect(). It establishes a connection with the specified database.
Following is an “if statement.” It is the part of the code that shows whether the connection was established. When the connection fails, it gives the
message Connection failed. The die function prints the message and then exits out of the script.
When the script ends, the connection with the database also closes. If you want to end the code manually, use the mysqli_close function.
The PHP code for connecting to a MySQL database through the PDO extension is:
<?php
$servername = "localhost";
$database = "database";
$username = "username";
$password = "password";
$charset = "utf8mb4";
try {
$dsn = "mysql:host=$servername;dbname=$database;charset=$charset";
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo
{
echo “Connection failed: ”. $e->getMessage();
}
?>
Credentials Syntax
First, we have five variables (server name, database, username, password, and charset) and their values. These values should correspond to your
connection details.
The server name will be localhost. If connected to an online server, type in the server name of that server.
The variable charset tells the database in which encoding it will be receiving and sending data. The recommended standard is utf8mb4.
If a problem arises while trying to connect, it stops running and attempts to catch and solve the issue. Catch blocks can be set to show error
messages or run an alternative code.
The first parameter in the try and catch block is DSN, which stands for data(base) source name. It is crucial as it defines the type and name of the
database, along with any other additional information.
In this example, we are using a MySQL database. However, PDO supports various types of databases. If you have a different database, replace that
part of the syntax (mysql) with the database you are using.
Next is the PDO variable. This variable is going to establish a connection to the database. It has three parameters:
1. PDO::ATTR_ERRMODE
2. PDO::ERRMODE_EXCEPTION
This method instructs the PDO to run an exception in case a query fails.
After returning the PDO variable, define the PDOException in the catch block by instructing it to display a message when the connection fails.
Possible solutions:
In case of other errors, make sure to consult the error_log file to help when trying to solve any issues. The file is located in the same folder where
the script is running.
Conclusion
This guide detailed two ways to connect to a MySQL database using PHP.
Both MySQLi and PDO have their advantages. However, bear in mind that MySQLi is only used for MySQL databases. Therefore, if you want to change
to another database, you will have to rewrite the entire code. On the other hand, PDO works with 12 different databases, whic h makes the migration
much easier.