You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -142,23 +145,23 @@ Attempts to connect to the remote MySQL server.
142
145
The `options` argument is a Lua table holding the following keys:
143
146
144
147
*`host`
145
-
: the host name for the MySQL server.
148
+
the host name for the MySQL server.
146
149
*`port`
147
-
: the port that the MySQL server is listening on. Default to 3306.
150
+
the port that the MySQL server is listening on. Default to 3306.
148
151
*`path`
149
-
: the path of the unix socket file listened by the MySQL server.
152
+
the path of the unix socket file listened by the MySQL server.
150
153
*`database`
151
-
: the MySQL database name.
154
+
the MySQL database name.
152
155
*`user`
153
-
: MySQL account name for login.
156
+
MySQL account name for login.
154
157
*`password`
155
-
: MySQL account password for login (in clear text).
158
+
MySQL account password for login (in clear text).
156
159
*`max_packet_size`
157
-
: the upper limit for the reply packets sent from the MySQL server (default to 1MB).
160
+
the upper limit for the reply packets sent from the MySQL server (default to 1MB).
158
161
*`pool`
159
-
: the name for the MySQL connection pool. if omitted, an ambiguous pool name will be generated automatically with the string template `user:database:host:port` or `user:database:path`. (this option was first introduced in `v0.08`.)
162
+
the name for the MySQL connection pool. if omitted, an ambiguous pool name will be generated automatically with the string template `user:database:host:port` or `user:database:path`. (this option was first introduced in `v0.08`.)
160
163
*`compact_arrays`
161
-
: when this option is set to true, then the [query](#query) and [read_result](#read_result) methods will return the array-of-arrays structure for the resultset, rather than the default array-of-hashes structure.
164
+
when this option is set to true, then the [query](#query) and [read_result](#read_result) methods will return the array-of-arrays structure for the resultset, rather than the default array-of-hashes structure.
162
165
163
166
Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method.
164
167
@@ -218,20 +221,24 @@ It returns a Lua table (`res`) describing the MySQL `OK packet` or `result set p
218
221
219
222
For queries corresponding to a result set, it returns an array holding all the rows. Each row holds key-value apirs for each data fields. For instance,
220
223
224
+
```lua
221
225
{
222
226
{ name="Bob", age=32, phone=ngx.null },
223
227
{ name="Marry", age=18, phone="10666372"}
224
228
}
229
+
```
225
230
226
231
For queries that do not correspond to a result set, it returns a Lua table like this:
227
232
233
+
```lua
228
234
{
229
235
insert_id=0,
230
236
server_status=2,
231
237
warning_count=1,
232
238
affected_rows=32,
233
239
message=nil
234
240
}
241
+
```
235
242
236
243
If more results are following the current result, a second `err` return value will be given the string `again`. One should always check this (second) return value and if it is `again`, then she should call this method again to retrieve more results. This usually happens when the original query contains multiple statements (separated by semicolon in the same query string) or calling a MySQL procedure. See also [Multi-Resultset Support](#multi-resultset-support).
237
244
@@ -273,9 +280,11 @@ It is always important to quote SQL literals properly to prevent SQL injection a
273
280
[ngx.quote_sql_str](http://wiki.nginx.org/HttpLuaModule#ngx.quote_sql_str) function provided by ngx_lua to quote values.
274
281
Here is an example:
275
282
283
+
```lua
276
284
localname=ngx.unescape_uri(ngx.var.arg_name)
277
285
localquoted_name=ngx.quote_sql_str(name)
278
286
localsql="select * from users where name = " ..quoted_name
287
+
```
279
288
280
289
Multi-Resultset Support
281
290
=======================
@@ -284,6 +293,7 @@ For a SQL query that produces multiple result-sets, it is always your duty to ch
284
293
285
294
Below is a trivial example for this:
286
295
296
+
```lua
287
297
localcjson=require"cjson"
288
298
localmysql=require"resty.mysql"
289
299
@@ -325,6 +335,7 @@ Below is a trivial example for this:
325
335
ngx.log(ngx.ERR, "failed to set keepalive: ", err)
326
336
ngx.exit(500)
327
337
end
338
+
```
328
339
329
340
This code snippet will produce the following response body data:
330
341
@@ -337,12 +348,14 @@ Debugging
337
348
338
349
It is usually convenient to use the [lua-cjson](http://www.kyne.com.au/~mark/software/lua-cjson.php) library to encode the return values of the MySQL query methods to JSON. For example,
339
350
351
+
```lua
340
352
localcjson=require"cjson"
341
353
...
342
354
localres, err, errcode, sqlstate=db:query("select * from cats")
343
355
ifresthen
344
356
print("res: ", cjson.encode(res))
345
357
end
358
+
```
346
359
347
360
Automatic Error Logging
348
361
=======================
@@ -351,7 +364,9 @@ By default the underlying [ngx_lua](http://wiki.nginx.org/HttpLuaModule) module
351
364
does error logging when socket errors happen. If you are already doing proper error
352
365
handling in your own Lua code, then you are recommended to disable this automatic error logging by turning off [ngx_lua](http://wiki.nginx.org/HttpLuaModule)'s [lua_socket_log_errors](http://wiki.nginx.org/HttpLuaModule#lua_socket_log_errors) directive, that is,
353
366
367
+
```nginx
354
368
lua_socket_log_errors off;
369
+
```
355
370
356
371
Limitations
357
372
===========
@@ -375,18 +390,22 @@ you do not need to do anything because it already includes and enables
375
390
lua-resty-mysql by default. And you can just use it in your Lua code,
376
391
as in
377
392
393
+
```lua
378
394
localmysql=require"resty.mysql"
379
395
...
396
+
```
380
397
381
398
If you are using your own nginx + ngx_lua build, then you need to configure
382
399
the lua_package_path directive to add the path of your lua-resty-mysql source
0 commit comments