What best practices should be followed when dealing with MySQL connections and queries in PHP for PDF generation?
When dealing with MySQL connections and queries in PHP for PDF generation, it is important to properly handle database connections, execute queries safely to prevent SQL injection, and close connections after use to free up resources. Using prepared statements can help mitigate SQL injection risks. Additionally, ensure error handling is in place to catch any potential issues with the database connection or queries.
// Establish a MySQL database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// Execute a query using prepared statements
$stmt = $conn->prepare("SELECT * FROM table WHERE column = ?");
$stmt->bind_param("s", $value);
$value = "example";
$stmt->execute();
$result = $stmt->get_result();
// Process the query results and generate PDF
// Close the database connection
$stmt->close();
$conn->close();