mariadb get insert id

From Onset Developer Wiki
Revision as of 08:47, 2 September 2020 by BlueMountains (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
mariadb get insert id

Type: Function
Context: Server
Introduced: v1.0

NOTICE

This function is provided by the official MariaDB plugin.

Description

Retrieves the ID generated for an AUTO_INCREMENT column by the sent query (usually INSERT).

Syntax

mariadb_get_insert_id()

Parameters

  • This function has no parameters.

Return Value

  • new ID generated for an AUTO_INCREMENT column, false on error.

Example

Example taken from: https://github.com/BlueMountainsIO/OnsetLuaScripts/blob/master/horizon/server/accounts.lua

function CreatePlayerAccount(player)
	local query = mariadb_prepare(sql, "INSERT INTO accounts (id, steamid, steam_name, game_version, locale, registration_time, registration_ip) VALUES (NULL, '?', '?', ?, '?', UNIX_TIMESTAMP(), '?');",
		tostring(GetPlayerSteamId(player)),
		GetPlayerName(player),
		GetPlayerGameVersion(player),
		GetPlayerLocale(player),
		GetPlayerIP(player))

	mariadb_query(sql, query, OnAccountCreated, player)
end


function OnAccountCreated(player)
	local new_id = mariadb_get_insert_id()

	if new_id == false then
		KickPlayer(player, "An error occured while creating your account 😨")
	else
		PlayerData[player].accountid = mariadb_get_insert_id()

		SetPlayerLoggedIn(player)

		print("Account ID "..PlayerData[player].accountid.." created for "..player)

		AddPlayerChat(player, '<span color="#ffff00aa" style="bold italic" size="15">SERVER: Welcome to the community, '..GetPlayerName(player)..', have fun and play fair!</>')
		AddPlayerChatAll('<span color="00ee00ff">We now have'..PlayerData[player].accountid..' accounts registered</>')
	end
end

See also