What are some best practices for using IF statements in PHP to check for database entries?

When using IF statements in PHP to check for database entries, it is important to properly handle the query results and ensure that the conditions are set up correctly to check for the existence of the desired data. One common practice is to use functions like mysqli_num_rows() to check if any rows were returned from the query. Additionally, it is recommended to sanitize user input before executing the query to prevent SQL injection attacks.

// Example of using an IF statement to check for database entries
$query = "SELECT * FROM users WHERE username = 'example_user'";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    // Data exists in the database for the specified username
    // Perform actions based on the query result
} else {
    // No data found for the specified username
    // Handle the case where no data exists
}