Skip to content

Commit 37d98f6

Browse files
Adrian Lynchdougwilson
Adrian Lynch
authored andcommitted
docs: add additional .query() documentation
closes mysqljs#1050
1 parent 57332ba commit 37d98f6

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

Readme.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,11 @@ space for a new connection to be created on the next getConnection call.
537537

538538
## Performing queries
539539

540-
In the MySQL library, the most basic way to perform a query is to call
541-
the `.query()` method on an object (like on a `Connection`, `Pool`, `PoolNamespace`
542-
or other similar objects).
540+
The most basic way to perform a query is to call the `.query()` method on an object
541+
(like on a `Connection`, `Pool`, `PoolNamespace` or other similar objects).
543542

544-
The simplest form on query comes as `.query(sqlString, callback)`, where a string
545-
of a MySQL query is the first argument and the second is a callback:
543+
The simplest form of .`query()` is `.query(sqlString, callback)`, where a SQL string
544+
is the first argument and the second is a callback:
546545

547546
```js
548547
connection.query('SELECT * FROM `books` WHERE `author` = "David"', function (error, results, fields) {
@@ -552,8 +551,8 @@ connection.query('SELECT * FROM `books` WHERE `author` = "David"', function (err
552551
});
553552
```
554553

555-
The second form `.query(sqlString, parameters, callback)` comes when using
556-
placeholders (see [escaping query values](#escaping-query-values)):
554+
The second form `.query(sqlString, values, callback)` comes when using
555+
placeholder values (see [escaping query values](#escaping-query-values)):
557556

558557
```js
559558
connection.query('SELECT * FROM `books` WHERE `author` = ?', ['David'], function (error, results, fields) {
@@ -580,6 +579,24 @@ connection.query({
580579
});
581580
```
582581

582+
Note that a combination of the second and third forms can be used where the
583+
placeholder values are passes as an argument and not in the options object.
584+
The `values` argument will override the `values` in the option object.
585+
586+
```js
587+
connection.query({
588+
sql: 'SELECT * FROM `books` WHERE `author` = ?',
589+
timeout: 40000, // 40s
590+
},
591+
['David'],
592+
function (error, results, fields) {
593+
// error will be an Error if one occurred during the query
594+
// results will contain the results of the query
595+
// fields will contain information about the returned results fields (if any)
596+
}
597+
);
598+
```
599+
583600
## Escaping query values
584601

585602
In order to avoid SQL Injection attacks, you should always escape any user

0 commit comments

Comments
 (0)