Can someone suggest a tutorial for working with databases in PHP?

Working with databases in PHP often involves using SQL queries to interact with the database. One popular way to do this is by using the PDO (PHP Data Objects) extension, which provides a data-access abstraction layer. By using PDO, you can connect to a database, execute queries, and fetch results in a secure and efficient manner.

// Connect to the database
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';
try {
    $dbh = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

// Execute a query
$stmt = $dbh->query('SELECT * FROM mytable');

// Fetch results
while ($row = $stmt->fetch()) {
    // Do something with the data
}

// Close the connection
$dbh = null;