Hello!
On Tue, Feb 4, 2014 at 1:17 AM, Pritam Patil wrote:
> i = 2
> while err == "again"
> res, err, errno, sqlstate = dbHandle\read_result
> if not res
> return nil
>
> ngx.say "ReadResult: "
> ngx.say type(res) --type is function, not table
>
I wonder why you are getting a Lua function value in the "res"
variable here. It should never happen if you are using the
lua-resty-mysql library. Which version of lua-resty-mysql and ngx_lua
(or just the version of ngx_openresty) are you using?
> 1. For 2 queries, it works with a hack of, executing the returned function
> in res.
> 2. Since the read_result is returning a nil value in err, result of 3rd
> query could not be retrieved (the sqlstate is nil too)
>
> Anything on this would be much helpful.
>
I'm not familiar with MoonScript but I've tried your MySQL query out
on my local machine with Lua:
location /t {
access_log off;
content_by_lua '
local cjson = require "cjson"
local mysql = require "resty.mysql"
local db = mysql:new()
db:set_timeout(1000) -- 1 sec
local ok, err, errno, sqlstate = db:connect({
host = "127.0.0.1",
port = 3306,
database = "world",
user = "ngx_test",
password = "ngx_test"})
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err, ": ",
errno, " ", sqlstate)
return ngx.exit(500)
end
local begin = ngx.now()
local res, err, errno, sqlstate =
db:query("show tables; show create table City; select
* from City limit 1")
if not res then
ngx.log(ngx.ERR, "bad result #1: ", err, ": ", errno,
": ", sqlstate, ".")
return ngx.exit(500)
end
ngx.say("result #1: ", cjson.encode(res))
local i = 2
while err == "again" do
res, err, errno, sqlstate = db:read_result()
if not res then
ngx.log(ngx.ERR, "bad result #2: ", err, ": ",
errno, ": ", sqlstate, ".")
return ngx.exit(500)
end
ngx.say("result #", i, ": ", cjson.encode(res))
i = i + 1
end
local ok, err = db:set_keepalive(10000, 50)
if not ok then
ngx.log(ngx.ERR, "failed to set keepalive: ", err)
ngx.exit(500)
end
';
}
Here I use the MySQL test database "world" because I don't have your
ABC database table.
Accessing /t gives the right response:
result #1: [{"Tables_in_world":"City"},{"Tables_in_world":"Country"},{"Tables_in_world":"CountryLanguage"}]
result #2: [{"Create Table":"CREATE TABLE `City` (\n `ID` int(11)
NOT NULL AUTO_INCREMENT,\n `Name` char(35) NOT NULL DEFAULT '',\n
`CountryCode` char(3) NOT NULL DEFAULT '',\n `District` char(20) NOT
NULL DEFAULT '',\n `Population` int(11) NOT NULL DEFAULT '0',\n
PRIMARY KEY (`ID`)\n) ENGINE=MyISAM AUTO_INCREMENT=4080 DEFAULT
CHARSET=latin1","Table":"City"}]
result #3: [{"ID":1,"District":"Kabol","Population":1780000,"Name":"Kabul","CountryCode":"AFG"}]
Best regards,
-agentzh