PHP ko MySQL database se connect karne ke liye aap MySQLi (MySQL Improved) ya PDO (PHP Data Objects) ka use kar sakte hain. Niche dono tarike diye gaye hain:

  1. MySQLi (Object-Oriented Approach)

<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "your_database_name";

// Create connection

$conn = new mysqli($servername, $username, $password, $database);

// Check connection

if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

}

echo "Connected succ

essfully";

?>

2. PDO (PHP Data Objects)

<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "your_database_name";

 

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->getMe

ssage();

}

?>