Openresty Front-end Development Introduction Five Mysql articles

Openresty Front-end Development Introduction Five Mysql Chapter

#### This chapter mainly demonstrates how to connect to mysql through lua, and obtain data from mysql according to the name entered by the user, and return it to the user. The

operation of mysql mainly uses the lua-resty-mysql library, the code It can be found on [github](https://github.com/openresty/lua-resty-mysql)

and there are also example codes on

it. Since the examples given on the official website are relatively basic and there are many codes, I will mainly introduce some here. How to encapsulate and simplify the code we call

lua/mysql.lua
```
local mysql = require "resty.mysql"

local config = {
    host = "localhost",
    port = 3306,
    database = "mysql",
    user = "root ",
    password = "admin"
}

local _M = {}


function _M.new(self)
    local db, err = mysql:new()
    if not db then
        return nil
    end
    db:set_timeout(1000) -- 1 sec

    local ok, err, errno, sqlstate = db:connect(config)

    if not ok then
        return nil
    end
    db.close = close
    return db
end

function close(self)
local sock = self. sock
    if not sock then
        return nil, "not initialized"
    end
    if self.subscribed then
        return nil, "subscribed state"
    end
    return sock:setkeepalive(10000, 50)
end

return _M
```

In fact, it is simply to do the connection with the close A simple encapsulation that hides the tedious initialization and connection pool details. Just call new, and redis will be automatically linked, and close will automatically use the connection pool

lua/hello.lua
```
local cjson = require "cjson"
local mysql = require "mysql"
local req = require "req"

local args = req.getArgs()

local name = args['name']

if name == nil or name == "" then
name = "root"
end

name = ngx.quote_sql_str(name) -- SQL escape, convert ' to \' to prevent SQL injection, and the escaped variable contains quotation marks, so it can be used directly as a condition value using

local db = mysql:new()

local sql = "select * from user where User = " .. name

ngx.say(sql)
ngx.say("<br/>")

local res, err, errno, sqlstate = db:query (sql)
db:close()
if not res then
ngx.say(err)
    return {}
end

ngx.say(cjson. encode(res))

```

Access
http://localhost/lua/hello?name=root

can get all users whose name is root in mysql. If there is no name parameter, the value of root is obtained by default.

From the output data, it can be seen that res is actually An array, and no matter how many pieces of data are returned, it is an array. When the result of our query is only one, we can get a record through res[1], and each row of data is a table, which can be obtained by column Name to get the value

ok, here we can get the value input by the user, and get the data from mysql, and then return the json data, we can already develop some simple interfaces

[sample code](https://github.com /362228416/openresty-web-dev) see demo5 section

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991629&siteId=291194637