9951 explained code solutions for 126 technologies


nginx-luaHow to execute Mysql query


init_by_lua_block {
  mysql = require "luasql.mysql"
}

server {
  location / {
    content_by_lua_block {
      local pool = mysql.mysql()
      local db = pool:connect('test', 'usr', 'pwd')
      
      local cursor = db:execute('SELECT * FROM table')
      row = cursor:fetch ({}, "a")
      while row do
        ngx.say(row.name)
        row = cursor:fetch ({}, "a")
      end
    }
  }
}ctrl + c
init_by_lua_block

nginx-lua module directive to run specified Lua code on server startup

mysql = require "luasql.mysql"

load Luarocks Mysql module

content_by_lua_block

nginx-lua module directive to specify block of Lua code

local pool = mysql.mysql()

init connection pool

pool:connect('test', 'usr', 'pwd')

connect to specified Mysql server

db:execute

execute specified SQL

cursor:fetch

return next row from Mysql results cursor

ngx.say

output given text to client


Usage example

local pool = mysql.mysql()
local db = pool:connect('test', 'usr', 'pwd')

local cursor = db:execute('SELECT NOW() as n')
row = cursor:fetch ({}, "a")
ngx.say(row.n)
output
2022-03-23 16:40:28