HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP MySQL Connect

Before you can store or retrieve any data from a MySQL database, you must first establish a connection. PHP provides three main ways to do this: **MySQLi (Object-Oriented)**, **MySQLi (Procedural)**, and **PDO**.


What You Need to Connect

To connect to a MySQL database, you generally need four pieces of information:

  • Servername: Usually "localhost" if the database is on the same server.
  • Username: Your database username (default is often "root").
  • Password: Your database password (default is often empty "" in local dev).
  • Database Name: (Optional for the initial connection, but needed to work with tables).

1. MySQLi (Object-Oriented)

This is a modern approach that uses PHP's object-oriented features. It is clean and widely used.

<?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 (Object-Oriented)";
?>

2. MySQLi (Procedural)

This approach uses standard functions instead of objects. It is often used by developers who prefer a more traditional programming style.

<?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 (Procedural)";
?>

3. PDO (PHP Data Objects)

PDO is the most flexible method because it works with many different database systems (not just MySQL). It also uses exceptions for error handling, which is a modern best practice.

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

    try {
        $conn = new PDO("mysql:host=$servername", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully (PDO)";
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
?>
Why PDO? PDO is considered more professional because it makes it much easier to switch your database from MySQL to something else (like PostgreSQL or SQL Server) later on.

Closing the Connection

While PHP automatically closes the connection when the script ends, it is good practice to close it manually to free up server resources as soon as you are done.

Method Syntax to Close
MySQLi (OO) $conn->close();
MySQLi (Proc) mysqli_close($conn);
PDO $conn = null;

Summary

  • MySQLi is specifically for MySQL databases.
  • PDO is a universal database driver that works with many systems.
  • Always include **error checking** to see if your connection succeeded.
  • Closing the connection manually is a good habit for performance.
Tip: For local development (like using XAMPP or Laragon), the default username is usually root and the password is "" (empty).

What's Next?

Now that we can connect to the server, let's learn how to create our first database using PHP in the **MySQL Create DB** lesson.