How to UPDATE in mysql database

__resource.lua

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

client_script 'client.lua'

server_script '@mysql-async/lib/MySQL.lua'
server_script 'server.lua'

This is, of course, an incredibly simple example. If you have any other client-side or server-side scripts to load, add them here. Just remember that dependencies come first! So, anything with variables that you want to use comes first.

Server.lua

MySQL.ready(function ()
    MySQL.Sync.execute("UPDATE users set Column='VALUE' WHERE identifier=@identifier", {['@identifier'] = 'VALUE/VARIABLE')
end)

Again, a simple example. Replace the ‘VALUE’ string you put there with the data you want to insert into the column you wish to update, and replace the end part where it says ‘VALUE/VARIABLE’ with whatever the variable is you want to find by. Here’s a simple, working example too…

MySQL.ready(function ()
    AddEventHandler('playerConnecting', function(playerName, setKickReason)
        identifiers = GetPlayerIdentifiers(source)
        MySQL.Sync.execute("INSERT INTO players (Steam, Username, PlayerID) VALUES (@steam,@username,@playerid)", {['@steam'] = identifiers[1], ['@username'] = playerName, ['@playerid'] = source})
    end)
end)

As you can see in my example here, it inserts data into the ‘players’ table every time a player joins the server. I have a variable ‘identifiers’ which holds all of the player’s identifiers, meaning identifiers[1] will return the SteamID that I want. Of course, this is an INSERT, not an update, but it follows the same sort’ve principle.

Hope I’ve been helpful, let me know if you need anything else :slight_smile:

2 Likes