mariadb get insert id: Difference between revisions

From Onset Developer Wiki
(Created page with "{{Info|Function|Server|1.0}} {{Notice|This function is provided by the official MariaDB plugin.}} {{FuncDescription|__EDIT_ME__}} {{FuncSyntax|mariadb_get_insert_id()}}...")
 
No edit summary
 
(One intermediate revision by one other user not shown)
Line 2: Line 2:
{{Notice|This function is provided by the official [[MariaDB]] plugin.}}
{{Notice|This function is provided by the official [[MariaDB]] plugin.}}


{{FuncDescription|__EDIT_ME__}}
{{FuncDescription|Retrieves the ID generated for an AUTO_INCREMENT column by the sent query (usually INSERT).}}


{{FuncSyntax|mariadb_get_insert_id()}}
{{FuncSyntax|mariadb_get_insert_id()}}
Line 9: Line 9:
{{FuncNoParam}}
{{FuncNoParam}}


{{FuncReturnValue|__EDIT_ME__}}
{{FuncReturnValue|new ID generated for an AUTO_INCREMENT column, '''false''' on error.}}


== Example ==
== Example ==
__EDIT_ME__
Example taken from: https://github.com/BlueMountainsIO/OnsetLuaScripts/blob/master/horizon/server/accounts.lua
<syntaxhighlight lang="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
</syntaxhighlight>


{{RelatedFunctions}}
{{RelatedFunctions}}
__EDIT_ME__
{{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