How can PHP be used to dynamically sort a MySQL table based on user input?

To dynamically sort a MySQL table based on user input using PHP, you can use a combination of PHP and SQL to construct the query based on the user's selection. The user input can be passed as a parameter to the PHP script, which then dynamically generates the SQL query with the specified sorting criteria.

// Assume $sortBy is the user input for sorting and $conn is the MySQL connection

$sortBy = $_GET['sortBy']; // Get the user input for sorting

// Construct the SQL query based on the user input
$sql = "SELECT * FROM table_name ORDER BY $sortBy";

$result = mysqli_query($conn, $sql);

// Process the query result as needed
while ($row = mysqli_fetch_assoc($result)) {
    // Output or process each row of data
}