What is the significance of the IF statement in the proposed SQL query?
The IF statement in the proposed SQL query is used to conditionally select a specific value based on a certain criteria. In this case, it allows for selecting either a default value or the actual column value depending on whether the column value is NULL or not. This can be useful in situations where you want to handle NULL values in a specific way.
// Proposed SQL query with IF statement
$sql = "SELECT id, name, IF(age IS NULL, 'N/A', age) AS age FROM users";
// Execute the query and fetch the results
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
}
} else {
echo "0 results";
}