×

Please Login or Register to continue.

3
(11K Views)

I need your help with my PHP project. My client has given a SQL file to me as the database, he wants filter search and fetch the data in pdf format which can printable with different page numbers.    

I am not an expert in PHP so pls can you guide me on how to handle this project.

just attaching that file..and also want to know that how to fetch multiple tables data from one database.. pls.
looking for the positive response 

(150 Points)
in PHP

Share

1 Answer
(4.3K Points)
2

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. 

How to generate PDF in PHP with MYSQL database

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. 


Comment
Thanks for posting your answer. It works.
shakti 12 Jan 2020

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..