How can PHP be used to sort numerical values in a database table?

To sort numerical values in a database table using PHP, you can use an SQL query with the ORDER BY clause. This clause allows you to specify the column to sort by and the order (ASC for ascending or DESC for descending). By executing this query in PHP, you can retrieve the sorted data from the database.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// SQL query to select and sort numerical values from a table
$sql = "SELECT * FROM table_name ORDER BY numerical_column ASC"; // Change ASC to DESC for descending order

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Value: " . $row["numerical_column"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>