Thanks for posting here. I am getting your question that you want to fetch MYSQL database data and generate PDF using PHP and MYSQL database. It's possible that you can get data from the MYSQL database and display it in PDF format. The PDF view will be managed by the PHP code. You will be abling to fetch many rows from MySQL database and generate a PDF file.
Let's start to generate PDF file using PHP and MYSQL database -
How to generate PDF file from the MYSQL database using PHP
First of all, you have to need to create a database table.
CREATE TABLE `records` (
`id` int(11) NOT NULL,
`name` varchar(40) NOT NULL,
`age` varchar(40) NOT NULL,
`mobile` varchar(100) NOT NULL
) ENGINE
We will fetch and generate the data above into a PDF file in PHP. To generate a PDF file using PHP, you have to insert some data into the MYSQL database table.
Let's insert some data into the MYSQL database table -
INSERT INTO `records` (`id`, `name`, `age`, `mobile`) VALUES
(1, 'Mohan', '29', '121219212'),
(2, 'Sohan', '23', '21191019281'),
(3, 'Jack', '45', '212121221'),
(4, 'Saim', '25', '2121212123'),
(5, 'Amenda', '32', '21212912912'),
(6, 'John', '21', '121212121');
All data are inserted well. Now we will fetch that data and generate a PDF file via PHP.
Let's create a confip.php file -
It will help to connect the MYSQL database to the PHP page. We will create a class inside this file. The class will be used to generate PDF file from the MYSQL database table.
config.php
<?php
class Db {
public $databaseHost = "localhost";
public $databaseUser= "root";
public $databasePassword = "";
public $databaseName = "tutorials";
function query($sql) {
$con = new mysqli($this->databaseHost,$this->databaseUser,$this->databasePassword,$this->databaseName);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$resultset[] = $row;
}
}
$con->close();
if(!empty($resultset))
return $resultset;
}
}
?>
Change the connection file credential according to your database.
Now, we are fetching data and generating pdf file using PHP.
Create another PHP file
index.php
<?php
require_once('config.php');
$db = new Db();
$res = $db->query("SELECT id,name,age,mobile FROM records");
$header = $db->query("SELECT UCASE(`COLUMN_NAME`)
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='tutorials'
AND `TABLE_NAME`='records'
and `COLUMN_NAME` in ('id','name','age','mobile')");
require_once('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica','B',17);
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(45,12,$column_heading,1);
}
foreach($res as $row) {
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(45,12,$column,1);
}
$pdf->Output();
?>
In the code above, we require fpdf file.
Download the fpdf file from URL http://www.fpdf.org/ and save it in fpdf folder.
The fpdf is a library. It will help you design a PDF view and download that pdf file using PHP.
In the code above, we are using the cell() function to define the table cells using values. We can set any font family for PDF view using SetFont().
These are created functions inside the fpdf file.
In this way, you can generate a PDF file MYSQL MySQL database using PHP.