In PHP, what are the best practices for handling calculations within a SELECT statement and displaying the results?

When performing calculations within a SELECT statement in PHP, it's best practice to use SQL functions for arithmetic operations and to alias the calculated columns for readability. Once the calculations are done in the SELECT statement, the results can be displayed using PHP to fetch and output the data.

<?php
// Connect to database
$conn = new mysqli($servername, $username, $password, $dbname);

// Query with calculation
$sql = "SELECT column1, column2, (column1 + column2) AS total FROM table_name";
$result = $conn->query($sql);

// Display results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column 1: " . $row["column1"]. " - Column 2: " . $row["column2"]. " - Total: " . $row["total"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();
?>