What are the advantages and disadvantages of using object-oriented versus procedural approaches in PHP when working with databases?

When working with databases in PHP, using an object-oriented approach can provide better organization, reusability, and maintainability of code. Object-oriented programming allows for the creation of classes and objects that represent database entities, making it easier to manage and manipulate data. On the other hand, a procedural approach may be simpler and more straightforward for smaller projects or tasks that do not require complex data structures.

// Object-oriented approach to working with databases in PHP
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 close() {
        $this->connection->close();
    }
}

// Example usage
$db = new Database('localhost', 'username', 'password', 'database');
$result = $db->query("SELECT * FROM table");
while ($row = $result->fetch_assoc()) {
    // Do something with the data
}
$db->close();