{"success":true,"name":"use-prepared-statements-for-every-php-database-query","description":"About to write a PHP query that includes any variable","category":"ai-brimind","path":"ai-brimind/use-prepared-statements-for-every-php-database-query/SKILL.md","file":"SKILL.md","content":"---\nname: use-prepared-statements-for-every-php-database-query\ndescription: \"About to write a PHP query that includes any variable\"\n---\n\n# Use Prepared Statements for Every PHP Database Query\n\n## When to use\nProject is PHP, and you are about to build an SQL string that contains a variable ($_GET, $_POST, function argument) via concatenation or interpolation.\n\n## Example request\n\"WHERE id = $id\" → any user-controlled value can rewrite the query; placeholders separate code from data — SQL injection. Escaping manually with quotes or addslashes → fragile and charset-dependent; PDO/mysqli prepare() already owns this — built-in mechanism. Column or table name from user input → placeholders can't bind identifiers; whitelist allowed names instead — allow-list validation. Same query run in a loop → prepare once, execute many; the driver already optimizes this — statement reuse.\n\n## Instructions\nYou are a PHP developer who treats string-built SQL as a defect, not a style choice. Before writing any query, check what already exists: does the project use PDO, mysqli, or an ORM/query builder (Eloquent, Doctrine)? Always route variables through the existing mechanism — named or positional placeholders with prepare()/execute(), or the builder's binding — never concatenation, interpolation, or manual escaping. If a dynamic part cannot be bound (table/column names, ORDER BY direction), do not interpolate it raw: validate it against a hardcoded whitelist and explain why. If you find existing concatenated queries while editing, flag them instead of copying the pattern. Only write the query once every variable's path into SQL is confirmed to go through binding or a whitelist, and state explicitly why the data can no longer change the query's structure.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #935\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}