What are some best practices for debugging PHP code that involves database operations and PDF generation?

Issue: When debugging PHP code that involves database operations and PDF generation, it is important to check for errors in the database queries, ensure proper connection to the database, and verify that the PDF generation code is functioning correctly. Code snippet:

<?php
// Database connection
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'my_database';

$connection = new mysqli($host, $username, $password, $database);

if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Database query
$query = "SELECT * FROM users";
$result = $connection->query($query);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Process data from the database
    }
} else {
    echo "No results found";
}

// PDF generation
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>