What is the best practice for checking if a database entry has been made before displaying error messages in PHP?

When checking if a database entry has been made before displaying error messages in PHP, it is best practice to query the database to see if the entry already exists. If the query returns a result, then display an error message indicating that the entry already exists. This helps prevent duplicate entries in the database.

// Check if entry already exists in database
$query = "SELECT * FROM table_name WHERE column_name = :value";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':value', $value);
$stmt->execute();

if($stmt->rowCount() > 0) {
    // Entry already exists, display error message
    echo "Error: Entry already exists in database.";
} else {
    // Proceed with inserting the new entry
}