What best practices should be followed when combining MySQL functions like DATE_FORMAT() with PHP queries to ensure efficient and accurate data representation?

When combining MySQL functions like DATE_FORMAT() with PHP queries, it is essential to properly format the date in MySQL to match the PHP date format. This ensures accurate data representation and avoids any discrepancies in the displayed dates. Additionally, using prepared statements in PHP can help prevent SQL injection attacks and improve the efficiency of the query execution.

<?php
// Establish a connection to the MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Prepare a SQL query with DATE_FORMAT() function
$sql = "SELECT id, name, DATE_FORMAT(date_column, '%Y-%m-%d') AS formatted_date FROM table_name";

// Execute the prepared statement
$result = $mysqli->query($sql);

// Fetch and display the results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Date: " . $row["formatted_date"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close the connection
$mysqli->close();
?>