### Instruction:
Develop a PHP script that connects to a MySQL database, retrieves all records from a table named "employees" with columns "id", "first_name", "last_name", "email", and "salary", and displays the results in an HTML table. Add functionality to sort the table by any column when its header is clicked.


### Response:

To create a PHP script that connects to a MySQL database, retrieves all records from a table named "employees" with columns "id", "first_name", "last_name", "email", and "salary", and displays the results in an HTML table, follow these steps:

1. Create a new PHP file (e.g., `index.php`).
2. Add the following code to connect to your MySQL database:

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

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

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

Replace `"your_username"`, `"your_password"`, and `"your_database"` with your actual MySQL credentials.

3. Add the following code to retrieve all records from the "employees" table and sort them based on the selected column:

```php
<?php
$order_by = isset($_GET['order_by']) ? $_GET['order_by'] : 'id';
$order_dir = isset($_GET['order_dir']) ? $_GET['order_dir'] : 'ASC';

$sql = "SELECT id, first_name, last_name, email, salary FROM employees ORDER BY {$order_by} {$order_dir}";
$result = $conn->query($sql);
?>
```

4. Add the following code to display the retrieved records in an HTML table:

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Employees</title>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <th><a href="?order_by=id&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">ID</a></th>
                <th><a href="?order_by=first_name&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">First Name</a></th>
                <th><a href="?order_by=last_name&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">Last Name</a></th>
                <th><a href="?order_by=email&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">Email</a></th>
                <th><a href="?order_by=salary&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">Salary</a></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $row["id"] . "</td>";
                    echo "<td>" . $row["first_name"] . "</td>";
                    echo "<td>" . $row["last_name"] . "</td>";
                    echo "<td>" . $row["email"] . "</td>";
                    echo "<td>" . $row["salary"] . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='5'>No records found</td></tr>";
            }
            ?>
        </tbody>
    </table>
</body>
</html>
```

5. Finally, close the MySQL connection:

```php
<?php
$conn->close();
?>
```

The complete `index.php` file should look like this:

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

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

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

$order_by = isset($_GET['order_by']) ? $_GET['order_by'] : 'id';
$order_dir = isset($_GET['order_dir']) ? $_GET['order_dir'] : 'ASC';

$sql = "SELECT id, first_name, last_name, email, salary FROM employees ORDER BY {$order_by} {$order_dir}";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Employees</title>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <th><a href="?order_by=id&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">ID</a></th>
                <th><a href="?order_by=first_name&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">First Name</a></th>
                <th><a href="?order_by=last_name&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">Last Name</a></th>
                <th><a href="?order_by=email&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">Email</a></th>
                <th><a href="?order_by=salary&order_dir=<?= $order_dir === 'ASC' ? 'DESC' : 'ASC' ?>">Salary</a></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $row["id"] . "</td>";
                    echo "<td>" . $row["first_name"] . "</td>";
                    echo "<td>" . $row["last_name"] . "</td>";
                    echo "<td>" . $row["email"] . "</td>";
                    echo "<td>" . $row["salary"] . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='5'>No records found</td></tr>";
            }
            ?>
        </tbody>
    </table>
</body>
</html>

<?php
$conn->close();
?>
```

Now, when you open `index.php` in your browser, it should display the "employees" table data and allow you to sort by any column by clicking on its header.