Skip to content

Commit 32fe3c4

Browse files
jaymcpjrfnl
andauthored
[Documentation]: WordPress.DB.PreparedSQL (#2454)
* Docs: add documentation for WordPress.DB.PreparedSQL --------- Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com>
1 parent d77b020 commit 32fe3c4

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0"?>
2+
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4+
title="Prepared SQL"
5+
>
6+
<standard>
7+
<![CDATA[
8+
When querying the database, you should use $wpdb->prepare() to escape and quote the contents of variables. This prevents SQL injection.
9+
Use placeholders for all variables used in the query. You should not use variable interpolation or concatenation.
10+
]]>
11+
</standard>
12+
<code_comparison>
13+
<code title="Valid: Placeholders with $wpdb->prepare() used for all variables in query.">
14+
<![CDATA[
15+
$wpdb->prepare(
16+
'SELECT * from table
17+
WHERE field = <em>%s</em>',
18+
<em>$_GET['foo']</em>
19+
);
20+
]]>
21+
</code>
22+
<code title="Invalid: Interpolated variables used in $wpdb->query().">
23+
<![CDATA[
24+
$wpdb->query(
25+
"SELECT * from table
26+
WHERE field = <em>{$_GET['foo']}</em>"
27+
);
28+
]]>
29+
</code>
30+
</code_comparison>
31+
32+
<code_comparison>
33+
<code title="Valid: Placeholders with $wpdb->prepare() used for all variables in query.">
34+
<![CDATA[
35+
$wpdb->prepare(
36+
'SELECT * from table
37+
WHERE field = <em>%s</em>',
38+
<em>$value</em>
39+
);
40+
]]>
41+
</code>
42+
<code title="Invalid: Concatenation of variables used in $wpdb->*().">
43+
<![CDATA[
44+
$wpdb->get_results(
45+
"SELECT * from table
46+
WHERE field = <em>" . $value</em>
47+
);
48+
]]>
49+
</code>
50+
</code_comparison>
51+
</documentation>

0 commit comments

Comments
 (0)