08. How to connect the database

08. How to connect the database

To connect a database in PHP, you can use the PHP Data Objects (PDO) extension or the MySQLi extension. Both extensions provide an interface for connecting to and interacting with databases. Here's a basic example of connecting to a MySQL database using PDO:

  1. PDO (Procedural Style):

    • Using PDO in the procedural style involves a series of function calls.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";

// Close connection
mysqli_close($conn);
?>
  1. PDO (Object-Oriented Style):

    • Using PDO in the object-oriented style involves creating an instance of the PDO class.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name

try {
    // Establish connection
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>
  1. MySQLi (Procedural Style):

    • Using MySQLi in the procedural style involves a series of function calls.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name

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

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";

// Close connection
mysqli_close($conn);
?>
  1. MySQLi (Object-Oriented Style):

    • Using MySQLi in the object-oriented style involves creating an instance of the mysqli class.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name

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

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

echo "Connected successfully";

// Close connection
$conn->close();
?>

Remember to replace the placeholders ($servername, $username, $password, and $dbname) with the actual values for your database in all four examples.

Choose the approach that best suits your coding style and requirements. Both PDO and MySQLi provide similar functionality for connecting to and interacting with databases in PHP, but PDO offers more database driver options and a consistent interface for working with different database systems.