1. Post #1
    Gold Member
    bobthe2lol's Avatar
    July 2007
    1,370 Posts
    How can i tell if my query returned nothing? like, i wana have it so if the player isnt i the databse, then add them. But if they are, then just print the database info on them. But it is never adding them, i'v tryed to do !result or not result or result == nill etc. but its always acting like there IS data there, but there isnt. Here's the code, and i posted this here because its not a newbie question :D
    require("tmysql")
    
    tmysql.initialize("localhost", "root", "mpass", "gmod_users", 3306, 1, 1)
    
    function callbackfunc(result, status, error)
    Msg("\nCallback function is now being run.\n")
    Msg("Query result:\n\n")
    if result then
    PrintTable(result)
    Msg("\n Status: "..status.."\n Error: "..error.."\n")
    
    else
    forkvin()
    end
    end
    
    function query(strquery)
    Msg("Query:\n "..strquery.."\n is now being run.")
    tmysql.query(strquery, callbackfunc)
    end
    
    for k, v in pairs(player.GetAll()) do
    	Msg("Player name: "..v:Nick().."\n Player steamid: "..v:SteamID().."\n")
       local strquery = "SELECT * FROM gmod_class WHERE steamid = '"..v:SteamID().."'"
       query(strquery)
    end
    
    function forkvin()
    Msg("\n Forkvin being run now.")
    for k, v in pairs(player.GetAll()) do
       local strquery = "INSERT INTO gmod_class (steamid, class) VALUES( '"..v:SteamID().."', 'guest')"
    Msg("Query:\n"..strquery.."\n")   
    query(strquery)
    end
    end
    

    Edited:

    The code's a bit messy :D

  2. Post #2
    bennyg's Avatar
    April 2007
    217 Posts
    Use Result[1]
    The first entry is always the row(if there isn't multiple) returned.

  3. Post #3
    Gold Member
    bobthe2lol's Avatar
    July 2007
    1,370 Posts
    Ok, so if !result[1]?

    Edited:

    or if result[1] == 0 or nill or something?
    Reply With Quote Edit / Delete Reply United States Show Events Friendly Friendly x 1 (list)

  4. Post #4
    Vir
    Vir's Avatar
    January 2008
    71 Posts
    Can't you simply check if result == {} then?
    Reply With Quote Edit / Delete Reply United Kingdom Show Events Disagree Disagree x 1 (list)

  5. Post #5
    Gold Member
    bobthe2lol's Avatar
    July 2007
    1,370 Posts
    hmmm, thats an idea. :D thanks.

    Edited:

    Nope. Still same exact thing

    require("tmysql")
    
    tmysql.initialize("localhost", "root", "a password", "gmod_users", 3306, 1, 1)
    
    function forkvin()
    Msg("\n Forkvin being run now.")
    for k, v in pairs(player.GetAll()) do
       local strquery = "INSERT INTO gmod_class (steamid, class) VALUES( '"..v:SteamID().."', 'guest')"
    Msg("Query:\n"..strquery.."\n")   
    query(strquery)
    end
    end
    
    function callbackfunc(result, status, error)
    Msg("\nCallback function is now being run.\n")
    Msg("Query result:\n\n")
    if result[1] == 0 then
    forkvin()
    else
    PrintTable(result)
    Msg("\n Status: "..status.."\n Error: "..error.."\n")
    end
    end
    
    function query(strquery)
    Msg("Query:\n "..strquery.."\n is now being run.")
    tmysql.query(strquery, callbackfunc)
    end
    
    for k, v in pairs(player.GetAll()) do
    	Msg("Player name: "..v:Nick().."\n Player steamid: "..v:SteamID().."\n")
       local strquery = "SELECT * FROM gmod_class WHERE steamid = '"..v:SteamID().."'"
       query(strquery)
    end
    
    and the console output is
    Code:
    Player name: x
     Player steamid: a steamid
    Query:
     SELECT * FROM gmod_class WHERE steamid = 'a steamid'
     is now being run.
    Callback function is now being run.
    Query result:
    
    
     Status: 1
     Error: 0
    Edited:

    i also tryed result == {} and i got the same thing again

  6. Post #6
    Gold Member
    Kogitsune's Avatar
    September 2005
    2,819 Posts
    if type( result ) == "table" then
    Reply With Quote Edit / Delete Reply United States Show Events Agree Agree x 1 (list)

  7. Post #7
    bennyg's Avatar
    April 2007
    217 Posts
    if type( result ) == "table" then
    Regardless if Data is found or not, it'll always return a table

    Use
    if !result[1] then
    //Insert stuff
    end
    
    Reply With Quote Edit / Delete Reply Germany Show Events Disagree Disagree x 1 (list)

  8. Post #8
    Gold Member
    Kogitsune's Avatar
    September 2005
    2,819 Posts
    Regardless if Data is found or not, it'll always return a table
    Considering that

    if tblX == { } then

    Should never run the code, I think my reply was correct :V:.

    It wasn't the solution to the thread, but rather a correction of the way to determine of the object is a table.
    Reply With Quote Edit / Delete Reply United States Show Events Agree Agree x 1 (list)

  9. Post #9
    Gold Member
    bobthe2lol's Avatar
    July 2007
    1,370 Posts
    It worked too well...
    Showing rows 0 - 29 (4,650 total, Query took 0.0004 sec)

    Ummm... it ran 4,650 times before i stoped it...

    Edited:

    How can i fix this?

    Edited:

    Ok, i made it not do that loop thing, but now its never going to the callbacfunc:

    require("tmysql")
    
    tmysql.initialize("localhost", "root", "nevar", "gmod_users", 3306, 1, 1)
    
    function forkvin()
    Msg("\n Forkvin being run now.")
    for k, v in pairs(player.GetAll()) do
       local strquery = "INSERT INTO gmod_class (steamid, class) VALUES( '"..v:SteamID().."', 'guest')"
    Msg("Query:\n"..strquery.."\n")   
    query(strquery)
    init()
    end
    end
    
    function callbackfunc(result, status, error)
    Msg("\nCallback function is now being run.\n")
    Msg("Query result:\n\n")
    if !result[1] then
    forkvin()
    else
    PrintTable(result)
    Msg("\n Status: "..status.."\n Error: "..error.."\n")
    end
    end
    
    function query(strquery)
    Msg("Query:\n "..strquery.."\n is now being run.")
    tmysql.query(strquery, callbackfunc)
    end
    
    function init()
    for k, v in pairs(player.GetAll()) do
    	Msg("Player name: "..v:Nick().."\n Player steamid: "..v:SteamID().."\n")
       local strquery = "SELECT * FROM gmod_class WHERE steamid = '"..v:SteamID().."'"
       query(strquery)
    end
    end
    concommand.Add( "Initmysql", init)
    
    And, as i said, its never going to the callback func, its just stopping at the query.
    Code:
    Player name: my name
     Player steamid: some steamid
    Query:
     SELECT * FROM gmod_class WHERE steamid = 'mah steamid'
     is now being run.
    Edited:

    What can i do about this?

  10. Post #10
    Gold Member
    AzuiSleet's Avatar
    September 2007
    758 Posts
    Count the result table. #result.

    The game doesn't process Think hooks unless there is a player in game, so on a dedicated server you need to add a bot or something to wake it up.