Connect to MySQL Database with PHP : A Step by Step Guide to Using MySQLi and PDO

 


There are mainly two ways to connect to a MySQL database: using MySQLi (MySQL Improved) and PDO (PHP Data Objects). Below are examples of both methods, along with explanations.


MySQLi Connection :

MySQLi can be used in both an object-oriented and procedural context. Here’s how you can connect to a database using both approaches:


Object-Oriented MySQLi :


Code
PHP

    
<?php

//Replace Following Values According to Your Development enviroment.
$servername = "localhost";
$username = "root";
$password = "";

//Create a database with name htu or replace with your once
$database_name = "htu";

// Create connection
$conn = new mysqli($servername, $username, $password, $database_name);

// Check connection
if ($conn->connect_error) {

//In PHP Die mean if the above code not working die stop execution and show a error.Next code will not execute
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

?>

    


In the object-oriented example, $conn is an instance of the mysqli class, and connect_error is used to check if the connection was successful.


Procedural MySQLi :


Code
PHP


<?php


$servername = "localhost";
$username = "root";
$password = " ";

//Create a Database with name of htu
$database_name = "htu";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database_name);

// Check connection
if (!$conn) {

//In PHP Die mean if the above code not working die stop execution and show a error.Next code will not execute
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


?>

  


In the procedural example, functions like mysqli_connect and mysqli_connect_error are used directly without creating an object.


PDO Connection:


Code
PHP




<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    // Set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>



  

PDO provides a data-access abstraction layer, which means that, regardless of which database you’re using, you use the same functions to issue queries and fetch data. PDO does not provide a database API specific to MySQL; hence, it can be used with any database supported by PDO.





In this PDO example, a try...catch block is used to handle any exceptions that may occur during the connection process.
Remember to replace "your_username", "your_password", and "your_database" with your actual database credentials and database name. Also, ensure that your PHP environment has the necessary extensions installed and enabled for MySQLi and PDO to work correctly.

Post a Comment

If You Have Any Doubts, Please Let Me Know and Share Your Opinion.

أحدث أقدم