Learn, How to connect the database and fetch the data from the database in MySQL and PHP.
Database you can import
-- phpMyAdmin SQL Dump
-- version 5.0.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Feb 05, 2021 at 08:02 PM
-- Server version: 10.4.17-MariaDB
-- PHP Version: 7.4.14
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `tutorial`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`username` varchar(130) NOT NULL,
`password` varchar(250) NOT NULL,
`created_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`, `password`, `created_at`) VALUES
(1, 'stint', 'password', '2021-02-05 23:50:18'),
(2, 'rakesh', 'password', '2021-02-05 23:50:18');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// connect to db
$host = "localhost";
$user = "root";
$password = "";
$database = "tutorial";
$conn = mysqli_connect($host, $user, $password, $database);
$results = $conn->query("SELECT * FROM users");
?>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Created at</th>
</tr>
<?php while ($data = $results->fetch_assoc()): ?>
<tr>
<td><?php echo $data['id'] ?></td>
<td><?php echo $data['username'] ?></td>
<td><?php echo $data['created_at'] ?></td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
I hope this will help you connect and fetch the data from the database PHP MySQL