Modules/MTA-MySQL/mysql insert id: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: __NOTOC__ Returns the value generated for an AUTO_INCREMENT field in the last query. For more information about when is this value updated visit http://dev.mysql.com/doc/refman/5.0/en/mys...)
 
mNo edit summary
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<pageclass class="#AA7592" subcaption="MTA-MySQL Module"></pageclass>
__NOTOC__
__NOTOC__
{{ModuleFunction|MTA-MySQL}}
Returns the value generated for an AUTO_INCREMENT field in the last query.
Returns the value generated for an AUTO_INCREMENT field in the last query.



Latest revision as of 17:44, 5 January 2011


Package-x-generic.png This function is provided by the external module MTA-MySQL. You must install this module to use this function.

Returns the value generated for an AUTO_INCREMENT field in the last query.

For more information about when is this value updated visit http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html

Syntax

int mysql_insert_id ( MySQLConnection handler )

Required arguments

  • handler: A valid MySQL link

Returns

The value generated for an AUTO_INCREMENT field in the last query.

Example

Example 1: This example creates an account for a player when they use /register, and tells them their database id.

function RegisterPlayer(playerSource, commandName, _password)
  local name = mysql_escape_string(handler, getPlayerName(playerSource)) -- Escape the strings to avoid SQL-Injection
  local password = mysql_escape_string(handler, _password)
  local query = "INSERT INTO account SET name='" .. name .. "', password=MD5('" .. password .. "')"

  if (mysql_query(handler, query)) then
    outputChatBox("Account created successfuly with id #" .. mysql_insert_id(handler), playerSource)
  else
    outputChatBox("An error has occured when trying to create your account.", playerSource)
  end
end

addCommandHandler("register", RegisterPlayer)

See also