How To Display Data From Database In PHP In Table
Last Updated : Mar 11, 2024
IN - PHP | Written & Updated By - Dikshita
In this article we will show you the solution of how to display data from database in PHP in table, here we needs to execute database connection to interact with database table data then needs to define sql query for select table data.
After that we have to connect this query with database connection so it will executes query at database then it returns all data now we have to display those data in table so we have to use loop for iterate each of them and print on webpage table.
Step By Step Guide On How To Display Data From Database In PHP In Table :-
Here we need to use mysqli_connect() method for execute connection with database ‘dbase’ and it refers ‘$conn’ then for select all data from table ‘info’ we defined select query and it stored on variable ‘$qry’ then connection $conn points select query ‘$qry’.
It will returns data of table ‘info’ records that is stored on variable $result.
In html block we defined table structure with header column names ‘ID, Name, Password’ then we using while loop for iterate all table data using ‘fetch_assoc()’.
Here variable ‘$row’ refers each table row data’s then we printing each column data using ‘echo’ within <td> column tag.
<?php $conn = mysqli_connect('localhost','root','','dbase'); if($conn){ echo "connected successfully"; } else{ echo "Error"; } $qry="SELECT * FROM info"; $result=$conn->query($qry); $conn->close(); ?> <html> <body> <table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Password</th> </tr> <?php while($rows=$result->fetch_assoc()) { ?> <tr> <td><?php echo $rows['ID'];?></td> <td><?php echo $rows['Name'];?></td> <td><?php echo $rows['Password'];?></td> </tr> <?php } ?> </table> </body> </html>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- Here we defined values ‘localhost,root,’’,’dbase’ for make connection with server database. Using mysqli_connect() method we executed database connection with those values and connection referred by variable ‘$conn’.
- Using if condition we are checking database connection whether exists or not then printed respective result on webpage.
- Defined select query with table name ‘info’ for select all row values from table and it is stored on variable ‘$qry’.
- We know query() method will executes query what we defined at database so we used this method for executes this select query at database by pointing with $conn connection variable then it returns either all collected table records or null that is stored on variable ‘$result’.
- For printing database table records we need html table structure so we defined table in html block with table headers ‘ID,Name,Password’.
- When collecting table records it contains more row of values so here we needs to use loop that’s way we are used while loop with fetch_assoc() method.
- Then we printed all row values column by column when specifying ‘ID,Name,Password’ column names with ‘$rows’ variable using echo statement between <td> tags.
Conclusion :-
In conclusion now we are able to know how to display data from database in table using php.
When work with php we need to create and process php files at server location and then we need to start the server before execute the program.
When we executing this program on browser it will displays all retrieved table ‘info’ records from database ‘dbase’ in html table on webpage.
I hope this tutorial on how to display data from database in PHP in table helps you and the steps and method mentioned above are easy to follow and implement.