All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Display PDF File In PHP From Database

Last Updated : Mar 11, 2024

How To Display PDF File In PHP From Database

In this tutorial we will show you the solution of how to display pdf file in PHP from database, for example, in some website let us upload from resume or certificate and what to show details saved in database in pdf format then we have to use this this article to see how to done that.

Here, we are going to assume that we already have create a database and a table from which we want to display pdf file.

Step By Step Guide On How To Display Pdf File In PHP From Database :-

Here, below our database name is database whereas table name used is pdf_info. Now, let us see the code below to display pdf file in php from database.

<?php
// Database Connection
$connect = mysqli_connect('localhost', 'username', 'password', 'database');
//Check for connection error
$query = "SELECT * FROM `pdf_info`";
$result = mysqli_query($connect,$query);
while($row = mysqli_fetch_assoc($result)){
  $pdf_file = $row->filename;
  $path_file = $row->directory;
}
?>
<div>
<?php
echo $path_file.$pdf_file;
?>
</div>
  1. Here, we will first have to create a database and a table inside database.
  2. Our database name here used is database whereas table name used is pdf_info.
  3. At next open your preferred IDE like VS code, notepad, etc. and create a file having extension .php.
  4. You can also use above code to see result. So, let us understand this.
  5. Here, we create basic structure of php codes in which <?php is our opening tag of php and ?> is closing tag of php. Inside these tags, we first create a database connection there. And that is done using mysqli_connect() function.
  6. Inside this function localhost is our hostname, then our username and password, last is our database name that is database. All this is stored in a variable named $connect.
  7. At next step, we retrieve connection or say our data from our table using query. If there is more than one pdf file then have to select specific file. We store this pdf data retrieved form database inside $query variable.
  8. Now, we check our connection using mysqli_query() function. if all is right then our retrieve connection gets store to $result variable.
  9. At last, we just fetch data using mysqli_fetch_assoc() function and store it into variables. You can also see the above example to see how to done this all.
  10. Now, to show pdf file we have to use echo our store data. We also can show pdf file in iframe, div, section, etc. One more thing there is one another way also to display pdf file in php from database.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to display pdf file in php from database.

I hope this tutorial on how to display pdf file in PHP from database helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪