How can virtual fields be utilized effectively in PHP queries to avoid adding new columns to tables?

When using virtual fields in PHP queries, you can avoid adding new columns to tables by dynamically calculating or generating the values you need within the query itself. This can be achieved by using expressions, functions, or aliases in the SELECT statement to create virtual fields on the fly without altering the table structure.

<?php
// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Query with virtual field
$sql = "SELECT id, name, age, (YEAR(CURDATE()) - birth_year) AS age_calculated FROM users";
$stmt = $pdo->query($sql);

// Fetch and display results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Age: " . $row['age'] . ", Calculated Age: " . $row['age_calculated'] . "<br>";
}
?>