Skip to content

Instantly share code, notes, and snippets.

@lhmwzy
Created December 26, 2013 02:38
Show Gist options
  • Save lhmwzy/8129067 to your computer and use it in GitHub Desktop.
Save lhmwzy/8129067 to your computer and use it in GitHub Desktop.
testdb.lua for agentzh
local mysql = require "resty.mysql"
--local cjson = require "cjson"
local db = mysql:new()
db:set_timeout(300000) -- 1 sec
local ok, err, errno, sqlstate = db:connect({
host = "127.0.0.1",
port = "3306",
database = "tel",
user = "root",
password = "yjiawje",
max_packet_size=1048})
if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
local query
query="select * from person limit 50 "
res, err, errno, sqlstate = db:query(query)
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
local rows=res
--ngx.say(cjson.encode(res))
for i, row in ipairs(rows) do
ngx.print("row ", i, ": ")
for col, val in pairs(row) do
if val ~= nil then
ngx.print(col, "=", val, " ")
else
ngx.print(col, "=null ")
end
end
ngx.say("<br>")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment