What are some best practices for handling and displaying numerical data retrieved from a MySQL database in PHP scripts?
When handling numerical data retrieved from a MySQL database in PHP scripts, it's important to ensure proper formatting and security measures are in place. To display numerical data accurately, it's recommended to use number_format() to format numbers with commas and decimal points. Additionally, always sanitize user input and escape output to prevent SQL injection attacks.
// Retrieve numerical data from MySQL database
$query = "SELECT * FROM table";
$result = mysqli_query($conn, $query);
// Display numerical data with proper formatting
while ($row = mysqli_fetch_assoc($result)) {
$number = number_format($row['numeric_column'], 2); // Format number with 2 decimal places
echo $number . "<br>";
}