What are the recommended methods for fetching and processing data from a MySQL database using PHP?

When fetching and processing data from a MySQL database using PHP, it is recommended to use the MySQLi or PDO extension for secure and efficient database operations. These extensions provide functions for connecting to the database, executing queries, and fetching results.

// Using MySQLi extension
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$result = $mysqli->query("SELECT * FROM table");
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Process data here
    }
} else {
    echo "No results found";
}

$mysqli->close();
```

```php
// Using PDO extension
try {
    $pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->query("SELECT * FROM table");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        // Process data here
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}