Skip to content

Commit 69f9247

Browse files
committed
Merge branch '2.13.x'
2 parents 1c7c4aa + f1453c5 commit 69f9247

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

docs/en/reference/data-retrieval-and-manipulation.rst

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ are then replaced by their actual values in a second step (execute).
8989
$sql = "SELECT * FROM articles WHERE id = ?";
9090
$stmt = $conn->prepare($sql);
9191
$stmt->bindValue(1, $id);
92-
$stmt->execute();
92+
$resultSet = $stmt->executeQuery();
9393
9494
Placeholders in prepared statements are either simple positional question marks (``?``) or named labels starting with
9595
a colon (e.g. ``:name1``). You cannot mix the positional and the named approach. You have to bind a parameter
@@ -108,7 +108,7 @@ position of the variable to bind into the ``bindValue()`` method:
108108
$stmt = $conn->prepare($sql);
109109
$stmt->bindValue(1, $id);
110110
$stmt->bindValue(2, $status);
111-
$stmt->execute();
111+
$resultSet = $stmt->executeQuery();
112112
113113
Named parameters have the advantage that their labels can be re-used and only need to be bound once:
114114

@@ -120,7 +120,7 @@ Named parameters have the advantage that their labels can be re-used and only ne
120120
$sql = "SELECT * FROM users WHERE name = :name OR username = :name";
121121
$stmt = $conn->prepare($sql);
122122
$stmt->bindValue("name", $name);
123-
$stmt->execute();
123+
$resultSet = $stmt->executeQuery();
124124
125125
The following section describes the API of Doctrine DBAL with regard to prepared statements.
126126

@@ -159,17 +159,28 @@ and methods to retrieve data from a statement.
159159
- ``bindParam($pos, &$param, $type)`` - Bind a given reference to the positional or
160160
named parameter in the prepared statement.
161161

162-
If you are finished with binding parameters you have to call ``execute()`` on the statement, which
163-
will trigger a query to the database. After the query is finished you can access the results
164-
of this query using the fetch API of a statement:
162+
If you are finished with binding parameters you have to call ``executeQuery()`` on the statement,
163+
which will trigger a query to the database. After the query is finished, a ``Doctrine\DBAL\Result``
164+
instance is returned and you can access the results of this query using the fetch API of the result:
165165

166-
- ``fetch($fetchStyle)`` - Retrieves the next row from the statement or false if there are none.
166+
- ``fetchNumeric()`` - Retrieves the next row from the statement or false if there are none.
167+
The row is fetched as an array with numeric keys where the columns appear in the same order as
168+
they were specified in the executed ``SELECT`` query.
167169
Moves the pointer forward one row, so that consecutive calls will always return the next row.
168-
- ``fetchColumn($column)`` - Retrieves only one column of the next row specified by column index.
170+
- ``fetchAssociative()`` - Retrieves the next row from the statement or false if there are none.
171+
The row is fetched as an associative array where the keys represent the column names as
172+
specified in the executed ``SELECT`` query.
169173
Moves the pointer forward one row, so that consecutive calls will always return the next row.
170-
- ``fetchAll($fetchStyle)`` - Retrieves all rows from the statement.
174+
- ``fetchOne()`` - Retrieves the value of the first column of the next row from the statement
175+
or false if there are none.
176+
Moves the pointer forward one row, so that consecutive calls will always return the next row.
177+
- ``fetchAllNumeric()`` - Retrieves all rows from the statement as arrays with numeric keys.
178+
- ``fetchAllAssociative()`` - Retrieves all rows from the statement as associative arrays.
179+
- ``fetchFirstColumn()`` - Retrieves the value of the first column of all rows.
171180

172-
The fetch API of a prepared statement obviously works only for ``SELECT`` queries.
181+
The fetch API of a prepared statement obviously works only for ``SELECT`` queries. If you want to
182+
execute a statement that does not yield a result set, like ``INSERT``, ``UPDATE`` or ``DELETE``
183+
for instance, you might want to call ``executeStatement()`` instead of ``executeQuery()``.
173184

174185
If you find it tedious to write all the prepared statement code you can alternatively use
175186
the ``Doctrine\DBAL\Connection#executeQuery()`` and ``Doctrine\DBAL\Connection#executeStatement()``
@@ -201,7 +212,7 @@ to the appropriate vendors database format:
201212
$date = new \DateTime("2011-03-05 14:00:21");
202213
$stmt = $conn->prepare("SELECT * FROM articles WHERE publish_date > ?");
203214
$stmt->bindValue(1, $date, "datetime");
204-
$stmt->execute();
215+
$resultSet = $stmt->executeQuery();
205216
206217
If you take a look at ``Doctrine\DBAL\Types\DateTimeType`` you will see that
207218
parts of the conversion are delegated to a method on the current database platform,
@@ -238,7 +249,7 @@ Since you are using an ``IN`` expression you would really like to use it in the
238249
$stmt = $conn->prepare('SELECT * FROM articles WHERE id IN (?)');
239250
// THIS WILL NOT WORK:
240251
$stmt->bindValue(1, array(1, 2, 3, 4, 5, 6));
241-
$stmt->execute();
252+
$resultSet = $stmt->executeQuery();
242253
243254
Implementing a generic way to handle this kind of query is tedious work. This is why most
244255
developers fallback to inserting the parameters directly into the query, which can open
@@ -309,7 +320,7 @@ Prepare a given SQL statement and return the
309320
310321
<?php
311322
$statement = $conn->prepare('SELECT * FROM user');
312-
$resultSet = $statement->execute();
323+
$resultSet = $statement->executeQuery();
313324
$users = $resultSet->fetchAllAssociative();
314325
315326
/*
@@ -342,13 +353,13 @@ executeQuery()
342353
~~~~~~~~~~~~~~
343354

344355
Creates a prepared statement for the given SQL and passes the
345-
parameters to the execute method, then returning the statement:
356+
parameters to the executeQuery method, then returning the result set:
346357

347358
.. code-block:: php
348359
349360
<?php
350-
$statement = $conn->executeQuery('SELECT * FROM user WHERE username = ?', array('jwage'));
351-
$user = $statement->fetchAssociative();
361+
$resultSet = $conn->executeQuery('SELECT * FROM user WHERE username = ?', array('jwage'));
362+
$user = $resultSet->fetchAssociative();
352363
353364
/*
354365
array(

docs/en/reference/security.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ Following are examples of using prepared statements with SQL and DQL:
105105
$sql = "SELECT * FROM users WHERE username = ?";
106106
$stmt = $connection->prepare($sql);
107107
$stmt->bindValue(1, $_GET['username']);
108-
$stmt->execute();
108+
$resultSet = $stmt->executeQuery();
109109
110110
// SQL Prepared Statements: Named
111111
$sql = "SELECT * FROM users WHERE username = :user";
112112
$stmt = $connection->prepare($sql);
113113
$stmt->bindValue("user", $_GET['username']);
114-
$stmt->execute();
114+
$resultSet = $stmt->executeQuery();
115115
116116
// DQL Prepared Statements: Positional
117117
$dql = "SELECT u FROM User u WHERE u.username = ?1";
@@ -133,7 +133,7 @@ are using just the DBAL there are also helper methods which simplify the usage q
133133
<?php
134134
// bind parameters and execute query at once.
135135
$sql = "SELECT * FROM users WHERE username = ?";
136-
$stmt = $connection->executeQuery($sql, array($_GET['username']));
136+
$resultSet = $connection->executeQuery($sql, array($_GET['username']));
137137
138138
There is also ``executeStatement`` which does not return a statement but the number of affected rows.
139139

0 commit comments

Comments
 (0)