How can PHP be used to create downloadable PDF reports from SQL databases?
To create downloadable PDF reports from SQL databases using PHP, you can use a library like TCPDF or FPDF to generate the PDF file. You would first query the database to retrieve the necessary data, then use the library to format and output the data into a PDF file. Finally, you can use PHP headers to force the browser to download the PDF file.
<?php
require('fpdf.php');
// Connect to database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
// Query database for data
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Create new PDF document
$pdf = new FPDF();
$pdf->AddPage();
// Output data into PDF
while($row = mysqli_fetch_assoc($result)) {
$pdf->Cell(0, 10, $row['column1'].' '.$row['column2'], 0, 1);
}
// Output PDF as download
$pdf->Output('D', 'report.pdf');
?>