|
5 | 5 | The *Database* package is designed to manage the operations of data |
6 | 6 | management through the use of a generic database engine. |
7 | 7 |
|
| 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 | + |
8 | 52 | ### Iterating on results |
9 | 53 |
|
10 | 54 | The `JDatabaseIterator` class allows iteration over |
11 | 55 | database results |
12 | 56 |
|
13 | 57 | ```php |
14 | 58 | $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(); |
16 | 62 | foreach ($iterator as $row) |
17 | 63 | { |
18 | 64 | // Deal with $row |
|
0 commit comments