Skip to content

Commit a666d9b

Browse files
committed
Allowing JDatabaseDriver::quote to take an array of strings.
If supplied with an array of strings, the quote method will return an array of quoted strings. Documentation for the escape and quote methods added. Unit test updated.
1 parent b79e348 commit a666d9b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

manual/en-US/chapters/packages/database.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,60 @@
55
The *Database* package is designed to manage the operations of data
66
management through the use of a generic database engine.
77

8+
### Escaping strings
9+
10+
Strings must be escaped before using them in queries (never trust any variable input, even if it comes from a previous database query from your own data source). This can be done using the `escape` and the `quote` method.
11+
12+
The `escape` method will generally backslash unsafe characters (unually quote characters but it depends on the database engine). It also allows for optional escaping of additional characters (such as the underscore or percent when used in conjunction with a `LIKE` clause).
13+
14+
The `quote` method will escape a string and wrap it in quotes, however, the escaping can be turned off which is desirable in some situations. The `quote` method will also accept an array of strings (added in 12.3) and return an array quoted and escaped (unless turned off) string.
15+
16+
```php
17+
function search($title)
18+
{
19+
// Get the database driver from the factory, or by some other suitable means.
20+
$db = JFactory::getDbo();
21+
22+
// Search for an exact match of the title, correctly sanitising the untrusted input.
23+
$sql1 = 'SELECT * FROM #__content WHERE title = ' . $db->quote($title);
24+
25+
// Special treatment for a LIKE clause.
26+
$search = $db->quote($db->escape($title, true) . '%', false);
27+
$sql2 = 'SELECT * FROM #__content WHERE title LIKE ' . $search;
28+
29+
//
30+
if (is_array($title))
31+
{
32+
$sql3 = 'SELECT * FROM #__content WHERE title IN ('
33+
. implode(',', $db->quote($title)) . ')';
34+
}
35+
36+
// Do the database calls.
37+
}
38+
```
39+
40+
In the first case, the title variable is simply escaped and quoted. Any quote characters in the title string will be prepended with a backslash and the whole string will be wrapped in quotes.
41+
42+
In the second case, the example shows how to treat a search string that will be used in a `LIKE` clause. In this case, the title variable is manually escaped using `escape` with a second argument of `true`. This will force other special characters to be escaped (otherwise you could set youself up for serious performance problems if the user includes too many wildcards). Then, the result is passed to the `quote` method but escaping is turned off (because it has already been done manually).
43+
44+
In the third case, the title variable is an array so the whole array can be passed to the `quote` method (this saves using a closure and a )
45+
46+
Shorthand versions are available the these methods:
47+
* `q` can be used instead of `quote`
48+
* `e` can be used instead of `escape`
49+
50+
These shorthand versions are also available when using the `JDatabaseQuery` class.
51+
852
### Iterating on results
953

1054
The `JDatabaseIterator` class allows iteration over
1155
database results
1256

1357
```php
1458
$dbo = JFactory::getDbo();
15-
$iterator = $dbo->setQuery($dbo->getQuery(true)->select('*')->from('#__content'))->getIterator();
59+
$iterator = $dbo->setQuery(
60+
$dbo->getQuery(true)->select('*')->from('#__content')
61+
)->getIterator();
1662
foreach ($iterator as $row)
1763
{
1864
// Deal with $row

0 commit comments

Comments
 (0)