Open In App

How to make a connection with MySQL server using PHP ?

Last Updated : 27 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL is a widely used database management system that may be used to power a wide range of projects. One of its main selling features is its capacity to manage large amounts of data without breaking a sweat. There are two approaches that can be used to connect MySQL and PHP code, which are mentioned below.

Let’s get to know both of these options a bit more.

A Database Abstraction Layer is the PHP Data Objects (PDO) extension. It functions as a user interface for the backend to interact with the MySQL database and make changes without altering the PHP code. It also allows you to deal with numerous databases at the same time. The main benefit of using PDO is that it keeps your code simple and portable.

MySQLi is a connector function that connects the PHP app’s backend to the MySQL database. It performs in the same way as the previous version, but it is safer and faster, with a more comprehensive collection of functions and extensions. With PHP 5.0.0, MySQLi was launched, and the drivers were installed with PHP 5.3.0. The API was created to work with MySQL versions 4.1.13 and higher.

Connect with MySQL: Before one can access data in the MySQL database, one needs to be able to connect to the server. Use the mysqli_connect() function according to your connecting way to establish a database connection. This function returns a database connection pointer also known as a database handle. This handle will be utilized later in the code. Remember to add your database credentials after you get the handle.

1. Connection via PDO – A database is also supplied in the PDO example below (“geeksDatabase”). To connect to a database, PDO requires a valid database else an exception is thrown.

PHP




<?php
  
$servername = "localhost";
$username = "user";
$password = "123456";
$dbName = "geeksDatabase";
  
try {
  
    // Creating the connection
    $conn = new PDO("mysql:host=$servername;dbname=$dbName"
                    $username, $password);
    
    // Setting the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connection established successfully...";
    
    // To close the connection
    $conn = null;
  
// If connection fails 
catch(PDOException $e) {
  
      // Throws the error message 
    echo "Connection failed: " . $e->getMessage();
}
  
?>


Output:

Connection established successfully...

2. Connection via MySQLi – 

PHP




<?php
  
$servername = "localhost";
$username = "user";
$password = "123456";
$dbName = "geeksDatabase";
  
// Creating connection
$conn = mysqli_connect($servername
         $username, $password, $dbName);
  
// Checking connection
if (!$conn) {
  
      // If connecting fails
    die("Connection failed: " . mysqli_connect_error());
}
  
echo "Connection established successfully...";
  
// Close the connection  
mysqli_close($conn);
  
?>


Output:

Connection established successfully...


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads