General > Developers Corner

Error in External MySQL database config - bad key

<< < (2/3) > >>

jacksop:
WOW! Thank you so much for the in-depth explanation. IT ALL MAKES SENSE NOW!

The server ran the function successfully. It all looks good!!

Thank you!


EDIT: 1 last thing... This function updates the data in the table every time a player disconnects. This means the if statement isn't working.

--- Code: ---if (#query2:getData() != currentulxrank) then

--- End code ---

After trying to print #query2:getData() using the following code I think I have nailed it down to the fact the function does not return a value other than nil.

--- Code: ---
local query2 = DB_SWRPDB:query( "SELECT ulx_role FROM player_information WHERE steam_id = '" .. plysteamid .. "';")
print(#query2:getData())


--- End code ---

Am I handling retrieving the contents of a table wrong? or is there something wrong with the function?

Here is the explanation of the function using FredyH's MYSQLOO manual (https://github.com/FredyH/MySQLOO)

--- Code: ---Query:getData()
-- Returns [Table]
-- Gets the data the query returned from the server
-- Format: { row1, row2, row3, ... }
-- Row format: { field_name = field_value } or {first_field, second_field, ...} if OPTION_NUMERIC_FIELDS is enabled

--- End code ---


This function would come in handy. Any ideas?

Timmy:
The query looks fine.

That if-statement looks odd.

There's a # in front of query2. The length operator (#) is used to get the length of a table or string.

Note that the GetData function returns a table of result rows. It takes a few more steps to access the rank.

Try to debug the select query a little further. Perhaps you could try something like this (untested)?

--- Code: ---query2.onSuccess = function()
    local results = query2:getData()
    local result = results[1] -- Get first result

    if result then
        PrintTable(result)

        if result.ulx_rank ~= currentulxrank then
            print("TODO: Update user rank from " .. result.ulx_rank .. " to " .. currentulxrank)
        else
            print("No update required")
        end
    else
        print("No results for this SteamID")
    end
end
--- End code ---

jacksop:
Oh ok thanks for clearing that up! Realised I used the wrong negation operator for lua!

After some playing around with the code you gave me I believe its working now!! THANKS!

I have another one of these functions but I don't understand why it isn't working. They are basically identical despite the job parameters. Just wanna keep track of this too.


--- Code: ---function DBSaveJobID (currentteam, steamid, nickname)
local query1 = DB_RP:query( "SELECT job_id FROM player_information WHERE steam_id = '" .. steamid .. "';")

query1.onSuccess = function()
local results = query1:getData()
local value = results[1]
if (value.job_id ~= currentteam) then
local query2 = DB_RP:query("UPDATE player_information SET job_id = '".. currentteam .."' WHERE steam_id = '".. steamid.."';")
query2.onSuccess = function()
MsgC( Color (0, 0, 255), "[SQL] The players job was logged \n")
end
query2.onError = function(db, err)
MsgC( Color (255, 0, 0), "[SQL] (Job Update) - Error: ", err)
end
query2:start()
end
end
query1:start()
end

--- End code ---

Thanks so much for helping this far...

Timmy:
Garry made some modifications to the Lua engine so you can actually use != to negate in GLua. It's not valid in regular Lua though. :D

I don't see any obvious mistakes in the last snippet you posted.

If the UPDATE query does not run, keep track of the value in currentteam. Does it update after you switch jobs? Perhaps you are confusing teams and jobs?

There will also be a Lua error if query1 returns no results. Make sure value is not nil before accessing job_id. Something like this will work:
if (value and value.job_id ~= currentteam) then

jacksop:
Ahhh right. That's handy.

I've tested what you said and changed the if statement to include the if value and value.job_id.
What happens is the query2 runs regardless of the if statement. The way I know this is everytime a player disconnects, the console prints:

[SQL] The players job was logged

...and the text for the UpdateULXRank function doesn't - therefore that if statement works but the save job if statement does not.

The function does replace the value in the database table under job_id to the correct team/job (I think job and team are related somehow...) - if I change jobs before I disconnect from the server, the number in the job_id column in the database changes. Also, if I print value.job_id it is the correct number that I am expecting.

This is really confusing because both functions should act the same.... weird...  ???

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version