How to Fetch Data from Database in PHP

0

In this Tutorial, We Will Learn How to Fetch Data from Database in PHP. These few simple steps to fetch data from database in PHP and MySQL. You know about the MySQL database, which used to store data write, and PHP is an OOP (Object Oriented Programming Language) Programing Language. I have seen many tutorials, they are using different methods to fetch data from the database.

I, don’t know which is best for you if you are a beginner or learner. I will share very helpful and easy methods to fetch data from the database. I hope you will easily understand it, after learning this tutorial you will be able to fetch data from database in PHP.

You May Also Like: Connect PHP to MySQL Tutorial

Fetch Data from Database in PHP

Below, I have shared PHP codes and also HTML5 Codes, you need to understand it. You can see them and you will use them the same way, I hope you will learn to understand it, How does work it? Don’t Worry I will make a video tutorial on this topic, you can learn step by step with the help of Tutorials.

How to Fetch Data from Database in Php and Display in HTML Table

Do you want to database values display in an HTML table, you can do it. Basically, I have seen many Web Applications in PHP, they are display database values in an HTML table. That’s very important to understand it because you will develop any Web Application, you need to display database values in HTML tables.

You May Also Like: Login Form in PHP Video Tutorial

Let’s get started to understand it, How You can do that? These few simple steps to fetch data from the database in PHP and display in HTML tables. First of All, you need to create a database and store manually values, then you need to create a connection between PHP and MySQL database.

Below I have shared the Connection codes in PHP, You can see it.

//connection.php File 
<?php 
mysql_connect('localhost','root',''); 
mysql_select_db('demo'); 
?>
// Index.php File 
<?php 
include_once('connection.php'); 
$query="select * from student"; 
$result=mysql_query($query); 
?> 
<!DOCTYPE html> 
<html> 
	<head> 
		<title> Fetch Data From Database </title> 
	</head> 
	<body> 
	<table align="center" border="1px" style="width:600px; line-height:40px;"> 
	<tr> 
		<th colspan="4"><h2>Student Record</h2></th> 
		</tr> 
			  <th> ID </th> 
			  <th> Name </th> 
			  <th> Email </th> 
			  <th> Country </th> 
			  
		</tr> 
		
		<?php while($rows=mysql_fetch_assoc($result)) 
		{ 
		?> 
		<tr> <td><?php echo $rows['ID']; ?></td> 
		<td><?php echo $rows['Name']; ?></td> 
		<td><?php echo $rows['Email']; ?></td> 
		<td><?php echo $rows['Country']; ?></td> 
		</tr> 
	<?php 
               } 
          ?> 

	</table> 
	</body> 
	</html>

After using the above codes, database values display in HTML tables as well. But you need to change the Database and Database Table Name.

You May Also Like: School Management System in PHP

Above All codes are very simple, I hope you have understood it. Furthermore, I have made a video tutorial on this topic, you can w tech it to learn step by step practically.

How to Fetch Data From Database in Php and Display

I hope you are understood How to Use Php to fetch data from the database in Php. If you want to learn How to Perform the operations such as insert update delete in Php, you can watch my video tutorial below. I’ve explained everything from scratch practically.

How to Display Data From Database in Php in Table

Guys, I’ve mentioned above the How to Fetch Data from Database in Php and Display in the HTML Table video tutorial and also the source code available on above. I going to share with you How to Display Data From the Database in the Table I am going to share with you another way you can use that to do that.

First of All, you need to make a database and also you need to make a table inside the database. You can make a table with any name and you need to assign the column How many columns you will create inside the database table? I’ve mentioned below SQL Query that helps you to make a table.

CREATE TABLE records 
( 
User_ID int AUTO_INCREMENT, 
User_Name varchar(50), 
User_Email varchar(50), 
User_Age varchar(20), 
)

Once you make the table inside the Database, you need to make a connection between Php and MySQLi database, I’ve mentioned below simple Php code that helps you to make a connection and also check it with your own computer. Inside the Connection, I’ve Database CRUD, if you have the same that’s perfect otherwise you need to change that.

<?php 
$con=mysqli_connect("localhost","root","","crud"); 
if(!$con) { die(" Connection Error "); } 
?>

Then you need to download the Bootstrap CSS File that helps you to design the table responsive and also attractive. Once you download Bootstrap CSS File, then you need to include that with your own project, once you add that, then you need to use the below code, but you need to change your bootstrap file inside the code.

<?php 

    require_once("connection.php");
    $query = " select * from records ";
    $result = mysqli_query($con,$query);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" a href="CSS/bootstrap.css"/>
    <title>View Records</title>
</head>
<body class="bg-dark">

        <div class="container">
            <div class="row">
                <div class="col m-auto">
                    <div class="card mt-5">
                        <table class="table table-bordered">
                            <tr>
                                <td> User ID </td>
                                <td> User Name </td>
                                <td> User Email </td>
                                <td> User Age </td>
                                <td> Edit  </td>
                                <td> Delete </td>
                            </tr>

                            <?php 
                                    
                                    while($row=mysqli_fetch_assoc($result))
                                    {
                                        $UserID = $row['User_ID'];
                                        $UserName = $row['User_Name'];
                                        $UserEmail = $row['User_Email'];
                                        $UserAge = $row['User_Age'];
                            ?>
                                    <tr>
                                        <td><?php echo $UserID ?></td>
                                        <td><?php echo $UserName ?></td>
                                        <td><?php echo $UserEmail ?></td>
                                        <td><?php echo $UserAge ?></td>
                                        <td><a href="#" class="btn btn-pencil">Edit</a></td>
                                        <td><a href="#" class="btn btn-danger">Delete</a></td>
                                    </tr>        
                            <?php 
                                    }  
                            ?>                                                                    
                                   

                        </table>
                    </div>
                </div>
            </div>
        </div>
    
</body>
</html>

If you have any questions/suggestions about this topic, kindly submit a single comment or email me. I will reply with your answer for a maximum of 24 hours. A subscriber to my Social Networks Pages and Emails for getting updates. Thanks for the read and watching my video Tutorial.

Previous articleHow to Create Login Form in PHP with MySQL Video Tutorial
Next articleBest Free Date Picker JQuery Calendar Plugin
I'm Programmer, Digital Marketer, and Blogger, I have been working on the web for 04 years. Basically, I have been sharing personal expertise on my website.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.