Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Documentation]: WordPress.DB.PreparedSQL #2454

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions WordPress/Docs/DB/PreparedSQLStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Prepared SQL"
>
<standard>
<![CDATA[
When querying the database, you should use $wpdb->prepare() to escape and quote the contents of variables. This prevents SQL injection.
Use placeholders for all variables used in the query. You should not use variable interpolation or concatenation.
]]>
</standard>
<code_comparison>
<code title="Valid: Placeholders with $wpdb->prepare() used for all variables in query.">
<![CDATA[
$wpdb->prepare(
'SELECT * from table
WHERE field = <em>%s</em>',
<em>$_GET['foo']</em>
);
]]>
</code>
<code title="Invalid: Interpolated variables used in $wpdb->query().">
<![CDATA[
$wpdb->query(
"SELECT * from table
WHERE field = <em>{$_GET['foo']}</em>"
);
]]>
</code>
</code_comparison>

<code_comparison>
<code title="Valid: Placeholders with $wpdb->prepare() used for all variables in query.">
<![CDATA[
$wpdb->prepare(
'SELECT * from table
WHERE field = <em>%s</em>',
<em>$value</em>
);
]]>
</code>
<code title="Invalid: Concatenation of variables used in $wpdb->*().">
<![CDATA[
$wpdb->get_results(
"SELECT * from table
WHERE field = <em>" . $value</em>
);
]]>
</code>
</code_comparison>
</documentation>
Loading