Explain the concept of 'SQL Injection' in the context of PHP's PDO. How do prepared statements actually prevent it at the protocol level?
Last updated
SQL Injection occurs when user input is concatenated into a query and changes its structure (e.g.
' OR '1'='1).Native prepared statements send the SQL template first so the server parses and plans it. Parameters then arrive in a separate packet as typed data, so they can never alter the fixed query structure.
Set
PDO::ATTR_EMULATE_PREPARESto false to get true server-side prepares. Table/column names can't be bound, so whitelist them.
- https://www.securityjourney.com/post/how-to-prevent-sql-injection-vulnerabilities-how-prepared-statements-work
- https://www.php.net/manual/en/security.database.sql-injection.php
- https://www.geeksforgeeks.org/sql/mitigation-sql-injection-attack-using-prepared-statements-parameterized-queries/
- https://sentry.io/answers/how-can-i-prevent-sql-injection-in-php/
- https://webreference.com/php/database/prepared-statements/
- https://www.php.net/manual/en/pdo.prepare.php
- https://www.php.net/manual/en/pdo.prepared-statements.php
- https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html