What are the differences in functionality between using procedural PHP code and OOP PHP code for accessing and displaying data from a database?

When accessing and displaying data from a database, using OOP PHP code offers better organization, reusability, and scalability compared to procedural PHP code. With OOP, you can create classes and objects that represent database connections, queries, and results, making it easier to manage and maintain your code. Additionally, OOP promotes encapsulation, inheritance, and polymorphism, which can lead to cleaner and more structured code.

<?php
// OOP PHP code for accessing and displaying data from a database

class Database {
    private $connection;

    public function __construct($host, $username, $password, $database) {
        $this->connection = new mysqli($host, $username, $password, $database);
        if ($this->connection->connect_error) {
            die("Connection failed: " . $this->connection->connect_error);
        }
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }

    public function displayData($result) {
        while ($row = $result->fetch_assoc()) {
            echo "Name: " . $row['name'] . "<br>";
            echo "Email: " . $row['email'] . "<br>";
            // Display other data fields as needed
        }
    }
}

// Usage example
$db = new Database('localhost', 'username', 'password', 'database_name');
$result = $db->query("SELECT * FROM users");
$db->displayData($result);

?>