PHP MySQL Connection | Connect PHP To MySQL Database

If you have to develop your application using PHP. you will have to stabilize PHP MySQL Connection. It helps to insert, retrieve, update & delete data in the database.

This tutorial will be helpful for learner developers as well as experienced developers. So, Not only experienced developers but also fresher can understand the concepts of this article.

Generally, most of the interviewers asked questions related to database connection. They expect the best answers from the candidates. Therefore they go into the depth of PHP Connect to MySQL. So, developers must read all the given steps. Because, every step contains more useful information.

php mysql connection
How to Connect MySQL database using PHP

 

Read Also

CRUD Operation in PHP Using MySQL

How to Connect PHP with MySQL Database

You have to connect PHP to MySQL through the following steps.

  • First of all, install Xampp /WapmServer on your pc
  • Start Local Server
  • Open PHPMyAdmin
  • Create Database in PHPMyAdmin
  • Create a project folder or PHP File
  • Write the following MYSQL query to connect the database using PHP.
  • MySQLi Procedural
  • MySqli Object-Oriented
  • PDO Connection

We need some information to connect database on local serve as

  • Server name or IP Addresslocalhost or 127.0.0.1 is the server name
  • username – root is the database username of the local database
  • password– no password of the local database
  • database name– custom database name of PHPMyAdmin

Now, I am going to share our types of database connection queries. you can use one of them according to your project.

1. MySQLi Procedural

This is a simple database connection query. If you are a learner and developing a small application then you can use it. It may be affected by SQL injection. So, It not secure.

Query –

<?php

$hostname     = "localhost"; // Enter Host Name
$username     = "root";      // Enter Dabase Username
$password     = "";          //Enter Database Password
$databasename = "mydatabase";// Enter Database Name

// Create connection
$conn = mysqli_connect($hostname, $username, $password,$databasename);

// Check connection
if (!$connection) {
    die("Unable to Connect database: " . mysqli_connect_error());
}
?>

 

2. MySQLi Object-Oriented

This is an advanced database connection query. Even it is more secure and protects the database from SQL injection. So. You should use this query to connect your project to the database.

Query –

 <?php

$hostname = "localhost"; // Enter Host Name 
$username = "root";      // Enter Dabase Username 
$password = "";         //Enter Database Password 
$databasename = "mydatabase";// Enter Database Name

// Create connection 
$conn = new mysqli($hostname, $username, $password,$databasename);
 
// Check connection
if ($connection->connect_error) {
 die("Unable to Connect database: " . $conn->connect_error);
 
?>

 

 

3. MySQL PDO

PDO– It is a PHP extension that stands for PHP Data Object. It is lightweight and easy to make a database connection.

If you want to move your project to another database, then you can easily do that by changing the server, database information & name of the database language only.

Connection Query –

<?php

$hostname     = "localhost"; // Enter host name
$username     = "root";       // Enter table username  
$password     = "";          // Enter table password
$databasename ="mydatabase";  // Enter database

try {
    $conn = new PDO("mysql:hostname=$hostname;databasename=$databasename", $username, $password);
    // set the PDO error mode
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "database is Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Unable to connect database: " . $e->getMessage();
    }
?>

 

4. MySQLi OOPS

If you want to create a PHP MySQL connection by using the MySQLi OOPS Concept. You will write the MySQLi Query with OOPS Concept.

You have many options to create a database connection using OPPS Concept. But here, We have created only a single way.

Connection Query –

<?php

class Connection{
  private $hostname ="localhost";
  private $username ="root";
  private $password ="";
  private $database ="mydatabase";
  
  public function __construct(){

    $conn=mysqli_connect($this->hostname,$this->username,$this->password,$this->database);

  if(!$conn){
    echo "database is not connected";
  }
  
  }
}

$mysql= new Connection();
?>

 

Database Connection File Inclusion

If you need to use your database connection on your other project files. you may include this file by using include() or require() method.

Suppose that, You write the PHP MySQL connection query in the file database.php. include it another file where you have to write PHP code with MySQL query.

My Suggestion

Dear Developers, If any questions are going in your mind related to PHP MySQL Connection. kindly, ask us without any hesitation through the comment box. I will definitely reply to your questions asap.