Displaying Data from Database Using PHP MySQLi
Displaying Data from Database as Table PHP mysqli fetch array |
Hi Guys in This Tutorial we will Learn how to Display Data from MySQL Database using PHP MySQLi.
Database Structure:
Database name: tutorialTable Name: test
CREATE TABLE IF NOT EXISTS `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(150) NOT NULL, `email` varchar(150) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `test` (`id`, `name`, `email`) VALUES (12, 'ANJANBD', 'Anjankumardhali6@gmail.com'), (11, 'Krishna', 'krishna@mail.com'), (3, 'Rupam Mondal', 'RupomMondal@gmail.com'), (4, 'ANJAN', 'KumarANjan@gmail.com'), (5, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'), (7, 'Hrithik', 'Hrithik@gmail.com'), (8, 'Modhu Bonik', 'Modhu360@gmail.com'), (9, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'), (13, 'Rupa', 'Rupa@hotmail.com'), (14, 'ANJAN', 'Kumar@gmail.com'), (15, 'ANJANBD', 'Anjankumardhali6@gmail.com'), (16, 'Krishna', 'krishna@mail.com'), (17, 'Rupam Mondal', 'RupomMondal@gmail.com'), (18, 'ANJAN', 'KumarANjan@gmail.com'), (19, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'), (20, 'Hrithik', 'Hrithik@gmail.com'), (21, 'Modhu Bonik', 'Modhu360@gmail.com'), (22, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'), (23, 'Rupa', 'Rupa@hotmail.com'), (24, 'ANJAN', 'Kumar@gmail.com'), (25, 'ANJANBD', 'Anjankumardhali6@gmail.com'), (26, 'ANJANBD60', 'Anjankumardhali6@gmail.comss'), (27, 'Priyoshi', 'Priyoshi@gmail.com'), (28, 'Rupom Chakma', 'Rupam@gmail.com'), (29, 'Monisha Roy', 'Monisharoy@gmail.com');
Directory or Folder and files Structure:
Create 3 files in any directory under htdocs folder.1.connect.php
2.index.php [Displaying Data]
3.style.css
Displaying Data from Database as Table PHP mysqli fetch array |
Codes for Connect.php
<?php $host = "localhost"; //Host name $username = "root"; //MySQL Username .'root' is default $password = ""; //MySQL Password $db_name="tutorial"; //Database Name // Create connection passing host,username,password and db_name $conn = new mysqli($host, $username, $password,$db_name); // Checking connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Codes for index.php
<link rel="stylesheet" href="style.css" type="text/css"/> <?php include("connect.php");; $sql = "SELECT id,name, email FROM test ORDER by id ASC "; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table>"; echo "<tr>"; echo "<th>ID</th>"; echo "<th>Name</th>"; echo "<th>Email</th>"; echo "</tr>"; // output data of each row while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>$row[id]</td>"; echo "<td>$row[name]</td>"; echo "<td>$row[email]</td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?>
Codes for style.css
table th, td{ border: 1px solid green; text-align:left; } th,td{ padding:8px; } table{ border-collapse:collapse; width:50%; } th{ padding:15px; background:lightgreen; font-size:20px; font-weight:bold; } tr:nth-child(odd){background:#808080} }
If you Like This article Please Like and Share With Your Friends.
Keep Visiting for More Codes.Thank you.
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.