Current Location: Home> Latest Articles> Tutorial on Developing an Employee Attendance Data Query Tool with PHP

Tutorial on Developing an Employee Attendance Data Query Tool with PHP

M66 2025-07-10

Setup Environment and Database Table Design

First, ensure PHP and MySQL are installed and running properly in your local environment. Next, create a MySQL database table to store employee attendance data. Below is an example SQL statement for creating the table:

CREATE TABLE attendance (
    id INT PRIMARY KEY AUTO_INCREMENT,
    emp_id INT NOT NULL,
    date DATE NOT NULL,
    time_in TIME NOT NULL,
    time_out TIME,
    status ENUM('Present', 'Absent') NOT NULL
);

Write Database Connection Code

Create a file named dbconn.php in your project root directory for database connection. The code is as follows:

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Replace the username, password, and database name with your actual credentials.

Implement Attendance Data Query Page

Create an index.php file in your project root directory as the employee attendance query interface. Example code:

<?php
include('dbconn.php');

$query = "SELECT * FROM attendance";
$result = $conn->query($query);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Employee Attendance Data Query Tool</title>
</head>
<body>
    <h1>Employee Attendance Data Query Tool</h1>
    <table border="1" cellspacing="0" cellpadding="5">
        <tr>
            <th>ID</th>
            <th>Employee ID</th>
            <th>Date</th>
            <th>Check-in Time</th>
            <th>Check-out Time</th>
            <th>Status</th>
        </tr>
        <?php
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['emp_id'] . "</td>";
                echo "<td>" . $row['date'] . "</td>";
                echo "<td>" . $row['time_in'] . "</td>";
                echo "<td>" . $row['time_out'] . "</td>";
                echo "<td>" . $row['status'] . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='6'>No data available</td></tr>";
        }
        ?>
    </table>
</body>
</html>

Running the Query Tool

Upload your project files to the web server root directory and open the index.php page in a browser to view employee attendance data. This tool displays all attendance records for easy management and queries.

Summary

Following these steps, you can quickly build a PHP and MySQL based employee attendance data query tool. The solution is well-structured and easy to expand, allowing you to customize the database design and query logic to fit various attendance management needs.