Skip to content

Commit 9476fad

Browse files
committed
Use ->prepare() instead of ->prepareStatement()
1 parent 6617b76 commit 9476fad

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

docs/php/database-access.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The database access is designed around [PreparedStatement](https://github.com/Wo
88

99
```php
1010
<?php
11-
$statement = \wcf\system\WCF::getDB()->prepareStatement("SELECT * FROM wcf".WCF_N."_example");
11+
$statement = \wcf\system\WCF::getDB()->prepare("SELECT * FROM wcf1_example");
1212
$statement->execute();
1313
while ($row = $statement->fetchArray()) {
1414
// handle result
@@ -22,10 +22,10 @@ The example below illustrates the usage of parameters where each value is replac
2222
```php
2323
<?php
2424
$sql = "SELECT *
25-
FROM wcf".WCF_N."_example
25+
FROM wcf1_example
2626
WHERE exampleID = ?
2727
OR bar IN (?, ?, ?, ?, ?)";
28-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
28+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
2929
$statement->execute([
3030
$exampleID,
3131
$list, $of, $values, $for, $bar
@@ -44,16 +44,16 @@ You can opt-in to retrieve only a single row from database and make use of short
4444
```php
4545
<?php
4646
$sql = "SELECT *
47-
FROM wcf".WCF_N."_example
47+
FROM wcf1_example
4848
WHERE exampleID = ?";
49-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql, 1);
49+
$statement = \wcf\system\WCF::getDB()->prepare($sql, 1);
5050
$statement->execute([$exampleID]);
5151
$row = $statement->fetchSingleRow();
5252
```
5353

5454
There are two distinct differences when comparing with the example on query parameters above:
5555

56-
1. The method `prepareStatement()` receives a secondary parameter that will be appended to the query as `LIMIT 1`.
56+
1. The method `prepare()` receives a secondary parameter that will be appended to the query as `LIMIT 1`.
5757
2. Data is read using `fetchSingleRow()` instead of `fetchArray()` or similar methods, that will read one result and close the cursor.
5858

5959
### Fetch by Column
@@ -65,8 +65,8 @@ Fetching an array is only useful if there is going to be more than one column pe
6565
```php
6666
<?php
6767
$sql = "SELECT bar
68-
FROM wcf".WCF_N."_example";
69-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
68+
FROM wcf1_example";
69+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
7070
$statement->execute();
7171
while ($bar = $statement->fetchColumn()) {
7272
// handle result
@@ -79,9 +79,9 @@ Similar to fetching a single row, you can also issue a query that will select a
7979
```php
8080
<?php
8181
$sql = "SELECT bar
82-
FROM wcf".WCF_N."_example
82+
FROM wcf1_example
8383
WHERE exampleID = ?";
84-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql, 1);
84+
$statement = \wcf\system\WCF::getDB()->prepare($sql, 1);
8585
$statement->execute([$exampleID]);
8686
$bar = $statement->fetchSingleColumn();
8787
```
@@ -95,8 +95,8 @@ To fetch all rows of query, you can use `PDOStatement::fetchAll()` with `\PDO::F
9595
```php
9696
<?php
9797
$sql = "SELECT *
98-
FROM wcf".WCF_N."_example";
99-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
98+
FROM wcf1_example";
99+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
100100
$statement->execute();
101101
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);
102102
```
@@ -108,8 +108,8 @@ If you only want to fetch a list of the values of a certain column, you can use
108108
```php
109109
<?php
110110
$sql = "SELECT exampleID
111-
FROM wcf".WCF_N."_example";
112-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
111+
FROM wcf1_example";
112+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
113113
$statement->execute();
114114
$exampleIDs = $statement->fetchAll(\PDO::FETCH_COLUMN);
115115
```
@@ -123,8 +123,8 @@ This case is covered by `PreparedStatement::fetchMap()`:
123123
```php
124124
<?php
125125
$sql = "SELECT exampleID, userID
126-
FROM wcf".WCF_N."_example_mapping";
127-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
126+
FROM wcf1_example_mapping";
127+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
128128
$statement->execute();
129129
$map = $statement->fetchMap('exampleID', 'userID');
130130
```
@@ -138,8 +138,8 @@ If you do not have a combination of columns with unique pairs of values, but you
138138
```php
139139
<?php
140140
$sql = "SELECT exampleID, userID
141-
FROM wcf".WCF_N."_example_mapping";
142-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
141+
FROM wcf1_example_mapping";
142+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
143143
$statement->execute();
144144
$map = $statement->fetchMap('exampleID', 'userID', false);
145145
```
@@ -171,10 +171,10 @@ Prepared statements not only protect against SQL injection by separating the log
171171
<?php
172172
$data = ['abc', 'def', 'ghi'];
173173

174-
$sql = "INSERT INTO wcf".WCF_N."_example
174+
$sql = "INSERT INTO wcf1_example
175175
(bar)
176176
VALUES (?)";
177-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
177+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
178178

179179
\wcf\system\WCF::getDB()->beginTransaction();
180180
foreach ($data as $bar) {
@@ -193,10 +193,10 @@ $data = [
193193
4 => 'ghi'
194194
];
195195

196-
$sql = "UPDATE wcf".WCF_N."_example
196+
$sql = "UPDATE wcf1_example
197197
SET bar = ?
198198
WHERE exampleID = ?";
199-
$statement = \wcf\system\WCF::getDB()->prepareStatement($sql);
199+
$statement = \wcf\system\WCF::getDB()->prepare($sql);
200200

201201
\wcf\system\WCF::getDB()->beginTransaction();
202202
foreach ($data as $exampleID => $bar) {

snippets/php/api/cronjobs/LastActivityCronjob.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function execute(Cronjob $cronjob) {
2323
SET user_table.lastActivityTime = session.lastActivityTime
2424
WHERE user_table.userID = session.userID
2525
AND session.userID <> 0";
26-
$statement = WCF::getDB()->prepareStatement($sql);
26+
$statement = WCF::getDB()->prepare($sql);
2727
$statement->execute();
2828
}
2929
}

snippets/php/code-style/Box.class.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class Box extends DatabaseObject {
1111
* @return Box|null
1212
*/
1313
public static function getBoxByIdentifier($identifier) {
14-
$sql = "SELECT *
15-
FROM wcf".WCF_N."_box
16-
WHERE identifier = ?";
17-
$statement = WCF::getDB()->prepareStatement($sql);
14+
$sql = "SELECT *
15+
FROM wcf1_box
16+
WHERE identifier = ?";
17+
$statement = WCF::getDB()->prepare($sql);
1818
$statement->execute([$identifier]);
1919

2020
return $statement->fetchObject(self::class);
2121
}
22-
}
22+
}

0 commit comments

Comments
 (0)