why in nginx lua "if not res" does not work when there is no record in mysql?

24 views
Skip to first unread message

zzhih...@gmail.com

unread,
Oct 11, 2018, 11:51:30 PM10/11/18
to openresty-en
here is my table in mysql:

tmp.png











my query code:
-- query
               local query_str = "select status from buystatus where uid='"..message["uid"].."'";
               ngx.log(ngx.NOTICE, "query string: ", query_str);
               local res, err, errno, sqlstate = db:query(query_str);
               if not res then
                       insert_str = "insert into buystatus values('"..message["uid"].."',"..message["status"]..")";
                       ngx.log(ngx.ERR, "insert string: ", insert_str);
                       db:query(insert_str);
                       local msg = {};
                       msg["code"]=200;
                       msg["msg"]="Insert a new record success.";
                       to_send=cjson.encode(msg);
                       ngx.say(to_send);
                       return
               end

                update_str = "update buystatus set status="..message["status"].." where uid='"..message["uid"].."'";
               db:query(update_str)

when I search a record uid is u888, then my code will execute  UPDATE part rather than INSERT part.

zzhih...@gmail.com

unread,
Oct 12, 2018, 3:37:58 AM10/12/18
to openresty-en
And I compare "#res" with zero instead to implement the same target.

在 2018年10月12日星期五 UTC+8上午11:51:30,zzhih...@gmail.com写道:

Bogdan Irimia

unread,
Oct 12, 2018, 5:34:07 AM10/12/18
to openre...@googlegroups.com
Log the value of "type(res)" to make sure you expect a null value or a string with 0 characters. I think the resulting variable "res", in case of error, is something in the lines of "ngx.null", but I'm not sure ("ngx.null" will have type "object"). This happens with Redis, because "ngx.null" can be a value in an array, whereas Lua's "nil" can't be.

zzhih...@gmail.com wrote:

And I compare "#res" with zero instead to implement the same target.

在 2018年10月12日星期五 UTC+8上午11:51:30,zzhih...@gmail.com写道:

    here is my table in mysql:

    tmp.png











    my query code:
    ||
    ||
    --query
    localquery_str ="select st atus from buystatus where

    uid='"..message["uid"].."'";
                   ngx.log(ngx.NOTICE,"query string: ",query_str);
    localres,err,errno,sqlstate =db:query(query_str);
    ifnotres then

                           insert_str ="insert into buystatus
    values('"..message["uid"].."',"..message["status"]..")";
                           ngx.log(ngx.ERR,"insert string: ",insert_str);
                         &nb sp; db:query(insert_str);
    localmsg ={};

                           msg["code"]=200;
                           msg["msg"]="Insert a new record success.";
                           to_send=cjson.encode(msg);
                           ngx.say(to_send);
    return
    end

                  &nbs p; update_str ="update buystatus set

    status="..message["status"].." where uid='"..message["uid"].."'";
                   db:query(update_str)

    when I search a record uid is u888, then my code will execute
    UPDATE part rather than INSERT part.

--
You received this message because you are subscribed to the Google
Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to openresty-en...@googlegroups.com
<mailto:openresty-en...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

zzhih...@gmail.com

unread,
Oct 12, 2018, 9:12:28 AM10/12/18
to openresty-en
I found another blog says that using "next(res)==nil" rather "#res" to see if res is empty or not.

在 2018年10月12日星期五 UTC+8下午5:34:07,bogdan写道:
Reply all
Reply to author
Forward
0 new messages