What potential pitfalls should be considered when handling database queries in PHP, especially when checking for existing records?

When handling database queries in PHP, especially when checking for existing records, potential pitfalls to consider include SQL injection attacks, data validation issues, and inefficient query performance. To mitigate these risks, it is important to use parameterized queries, sanitize user input, and optimize queries for better performance.

// Example of using parameterized queries to check for existing records in a database

// Assume $conn is the database connection object

// Sanitize user input
$user_input = filter_var($_POST['user_input'], FILTER_SANITIZE_STRING);

// Prepare a parameterized query
$stmt = $conn->prepare("SELECT * FROM table_name WHERE column_name = ?");
$stmt->bind_param("s", $user_input);
$stmt->execute();

// Check for existing records
$result = $stmt->get_result();
if($result->num_rows > 0) {
    // Record exists
} else {
    // Record does not exist
}

$stmt->close();
$conn->close();