mariadb get insert id: Difference between revisions

From Onset Developer Wiki
28days (talk | contribs)
No edit summary
No edit summary
 
Line 12: Line 12:


== Example ==
== Example ==
Example taken from: https://github.com/BlueMountainsIO/OnsetLuaScripts/blob/master/horizon/server/accounts.lua
<syntaxhighlight lang="Lua>
<syntaxhighlight lang="Lua>
local player_sqlid = mariadb_get_insert_id()
function CreatePlayerAccount(player)
print("A new player with #"..player_sqlid.." has registered.")
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
</syntaxhighlight>
</syntaxhighlight>


{{RelatedFunctions}}
{{RelatedFunctions}}
{{Template:MariaDBFunctions}}
{{Template:MariaDBFunctions}}

Latest revision as of 08:47, 2 September 2020

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