How To Connect Database With Example In PHP MYSQLI

admin_img Posted By Bajarangi soft , Posted On 22-09-2020

connect to MySQL database to access data from database on web page so we need to create connection between database and web page

How to connect MySQL database with PHP

There are three different ways to connect database in PHP

  1. MySQLi Procedural

  2. MySQLi Object-Oriented

  3. PDO

Example(1) MySQLi Procedural

<?php
$servername = "localhost";
$username = "username";
$password = "password";

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

In Procedural MySqli connection directly connected to database

Example(2) MySQLi Object-Oriented

<?php
$servername = "localhost";
$username = "username";
$password = "password";

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

In MySQLi Object-Oriented connection are done by objects .

Example(3) PDO

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $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 is that it has an exception class to handle any problems that may occur in our database queries. If an exception is thrown within the try{ } block, the script stops executing and flows directly to the first catch(){ } block.

Complete code for MySQL database with PHP

<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to connect MySQL database with PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Connect MySQL database with PHP</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");

        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }

        if (!$connect) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "MySQLi Procedural Connected successfully";

        echo "<br>";
        $servername = "localhost";
        $username = "root";
        $password = "";
        $db = "school_management";

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

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        echo "MySQLi Object-Oriented Connected successfully";

        echo "<br>";

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

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

Related Post