Home / Learn Secure SQL / Escaping vs Validation: Two Different Tools

Escaping vs Validation: Two Different Tools

Why "I escaped it" is not the same claim as "I validated it".

Bottom lineWhy "I escaped it" is not the same claim as "I validated it".

Breaking it down

Escaping vs validationEscapingTransforms a value for aspecific output contextContext-specific; the rightfunction mattersValidationChecks a value matchesexpectations before useContext-independent; aboutcorrectness
Escaping vs validation

What it is

Escaping transforms a value so special characters lose their meaning in a specific output context (like HTML); validation checks whether a value matches expectations before it is used at all. They solve different problems and are not interchangeable.

Why it matters

Manually escaping a value for SQL yourself (writing your own function to double quotes, for example) is fragile and strongly discouraged; prepared statements make manual SQL-escaping unnecessary entirely, which is why this course has barely mentioned it until now.

Mechanically speaking

For SQL specifically: use prepared statements, never manual escaping. For other output contexts (HTML, shell arguments), use that context's own proper escaping or encoding function, matched to where the value is actually going.

Shows up in

  • Web application code review
  • Backend development
  • Database administration
Careful:Avoid hand-written SQL-escaping functions (replacing quote marks yourself, for example). They are a known source of subtle bugs across many languages and database encodings; prepared statements remove the need for this entirely.

Check yourself

1. For SQL specifically, the recommended approach is...