Introduction:
HTML tables are a powerful tool for organizing and presenting data on web pages. When combined with PHP, you can dynamically generate tables and populate them with data from a MySQL database. In this article, we will walk through the process of creating an HTML table using PHP and MySQL, step by step.
Step 1: MySQL Query Creation
Before we start displaying data in an HTML table, we need to establish a connection to the MySQL database and retrieve the necessary data using SQL queries.
We have a table named "users" with columns such as "id", "name," and "email."
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
Insert some sample records into the table for testing.
Step 2: Create PHP files
Create a file "index.php" and insert the below code,
<!DOCTYPE html>
<html>
<head>
<title>Users Table</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
</style>
</head>
<body>
<h2>Users Table</h2>
<?php include 'display_data.php'; ?>
</body>
</html>
Create a file called "display_data.php" and insert the code as shown below,
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phpsamples";
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to retrieve users data
$sql = "SELECT * FROM users";
// Execute the query
$result = $conn->query($sql);
// Close the connection
$conn->close();
?>
<?php if ($result->num_rows > 0): ?>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php else: ?>
<p>No users found.</p>
<?php endif; ?>
In this example, we assume there is a MySQL database with a table named users
that contains columns for id
, name
, and email
. Make sure to replace your_username
, your_password
, your_database
, and users
with your actual database credentials and table name in the PHP code.
When you run the PHP file (index.php
) in a web browser, the PHP code will execute, establish a connection to the database, retrieve the user data, and generate an HTML table dynamically. If no users are found, it will display a "No users found" message.
Step 3: Test the Application
To test the application and see the output, follow these steps:
Start your local development server (e.g., Apache in XAMPP). Open a web browser and navigate to the project e.g. http://localhost:8080/PHPHtmlTable/index.php.
If everything is set up correctly, when you access the "index.php" file through a web browser, you will see an HTML table displaying the user data retrieved from the database. The table will have columns for ID, name, and email, and each row will represent a user record.
Conclusion:
In this article, we have learned how to display data with an HTML table in PHP. By connecting to a MySQL database, executing SQL queries, and using PHP to generate HTML code, we can dynamically present data in a tabular format. This approach allows for flexibility in displaying data from various database tables and is widely used in web development.
Comments (0)