Fetch Data from mysql table
1)Create a database named suman.
2)Then create a table named student.Use the below sql(id should be auto increment and primary:
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
—
— Indexes for dumped tables
—
—
— Indexes for table `student`
—
ALTER TABLE `student`
ADD PRIMARY KEY (`id`);
—
— AUTO_INCREMENT for dumped tables
—
—
— AUTO_INCREMENT for table `student`
—
ALTER TABLE `student`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
COMMIT;
3)Insert some data to student table
4)then create a page named 'index.php' with the below code:
<?php $conn =new mysqli('localhost', 'root', '' , 'suman'); $sql = "SELECT * FROM student"; $result = $conn->query($sql); $conn->close(); ?> <html> <head> <title>Student Listing</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" /><!-- Optional theme --> <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/js/fontawesome.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <table width="100%" align="center"> <tr> <td style="text-align:right;" colspan="2"><a href="add.php">Add New Student<i class="far fa-add"></i></a></td> </tr> </table> <div style="clear: both;margin-top: 18px;"> <table id="myTable" class="table table-striped table-bordered" style="width:100%"> <thead> <tr> <th>Name </th> <th>Email </th> <th>Action</th> </tr> </thead> <tbody> <?php if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { ?> <tr> <td align="center"><?php echo $row["name"]; ?></td> <td align="center"><?php echo $row["email"]; ?></td> <td align="center"><a href="edit.php?id=<?php echo $row["id"]; ?>"><i class="far fa-edit"></i></a> <a href="delete.php?id=<?php echo $row["id"]; ?>"><i onclick="return confirm('Are you sure you want to delete this item?');" class="far fa-trash-alt"></i></a></td> </tr> <?php } } ?> </tbody> </table> </div> </body> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.6.0/dt-1.11.4/af-2.3.7/b-2.2.2/b-html5-2.2.2/r-2.2.9/sp-1.4.0/datatables.min.css"/> <script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.6.0/dt-1.11.4/af-2.3.7/b-2.2.2/b-html5-2.2.2/r-2.2.9/sp-1.4.0/datatables.min.js"></script> <style> .page-item.active .page-link { background-color: lightgrey !important; border: 1px solid black; } .page-link { color: black !important; } </style> <script> $(document).ready( function () { $('#myTable').DataTable(); } ); </script> </html>
5)Create a folder named test inside htdocs folder.Put the index.php file inside test folder.
6)start xampp apache and Mysql service
7)Run the code http://localhost/test/index.php