What are the best practices for storing currency values in MySQL using PHP?

When storing currency values in MySQL using PHP, it is important to use the appropriate data type to ensure accuracy and prevent rounding errors. The recommended data type for storing currency values is DECIMAL with a specified precision and scale. This allows for precise storage of monetary values without losing precision.

// Example of storing currency values in MySQL using PHP
$amount = 100.50; // Currency value to be stored
$amount = number_format($amount, 2, '.', ''); // Format the currency value to 2 decimal places

// Insert the currency value into the database
$query = "INSERT INTO transactions (amount) VALUES ('$amount')";
$result = mysqli_query($connection, $query);

if ($result) {
    echo "Currency value stored successfully";
} else {
    echo "Error storing currency value";
}