tmysql problems

So in my gamemode when someone creates a character they input a citizen id. The citizen id is the primary key for my data tables so i don’t ever want duplicates. (even though I couldn’t…)

To assure that none of this will happen I am attempting to try and select the cid out of the database to see if it actually exists and isn’t nil.

[LUA]
local cid = args[3]
if Liquid.IssueCID(ply,cid) == false then
Liquid.Notify(ply, “Error: Citizen I.D. is already in use! Choose Another!”)
return
end
[/LUA]
This is in the console command where I am calling the database function to do the checking for me and return a value of true/false.

[LUA]
function Liquid.IssueCID(ply,cid)
local check = tmysql.query(“SELECT cid FROM liquidrp_characters WHERE cid=’”…tostring(cid)…"’", function(res)
if !res[1] or !res[1][“cid”] or !res then
return true
else
return false
end
end, 1);
return check
end
[/LUA]

However, the probem is, its still just trying to add it anyway like its not working. Would anyone happen to know what is wrong with these two blocks of code?

!res[1][“cid”] should be !res[1][1] and to learn more about how the table structure you get from tmysql and gm_mysql use this code:

PrintTable(res)

NOTE: i have no knowledge about tmysql but it should return same table structure

You should check the first, most likely thing to fail when checking results. In this case, you should check that result is valid first, before attempting to access members inside it. You should also return false if the inner-function handler does not handle the query properly to prevent overwriting as a fail-safe. Something like:

[lua]function Liquid.IssueCID ( ply, cid )
tmysql.query( “SELECT cid FROM liquidrp_characters WHERE cid=’” … tostring( cid ) … “’”,
function ( result )
– check if exists, return false if it does
if result and result[1] and result[1][1] then return false; end
– doesnt exist, return true
return true;
end
);
– fail-safe
return false
end[/lua]

i think this would be even better since result is a 2d table

function Liquid.IssueCID ( ply, cid )
    tmysql.query( "SELECT `cid` FROM `liquidrp_characters` WHERE `cid`='" .. tostring( cid ) .. "'",
        function ( result )
            -- check if exists, return false if it does
            if result and result[1][1] then return false; end
            -- doesnt exist, return true
            return true;
        end
    );
    -- fail-safe
    return false
end

This ends up just always making it false :confused:

EDIT: Ill try yours yuriman.

[editline]10:37PM[/editline]

Both still do not work, what the…

Are your CIDs just normal text? You may want to force lower-case or upper-case compares and statements to help ensure people don’t mix casings to try to overwrite someone else.

Is there a reason you want people to pick a CID rather then just use a auto-incrementing id or something?

try to do printTable(result) and give us the result

None of those will work.

TMySQL is a threaded library which means the function in the query will not be called until the query is complete, Lua just continues on while the query runs in a seperate thread. Once the query is complete, the function is called. You need to learn how to use callbacks, returns will not work.