Modules/MTA-MySQL/mysql change user: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
 
Line 22: Line 22:
'''Example 1:'''
'''Example 1:'''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function resourceStart ( resource )
function resourceStart ( res )
   if (resouce == getThisResource()) then
   if (res == getThisResource()) then
     myhandler = mysql_connect("localhost", "writer_user", "password", "mta_users") -- Start with a read-write username
     myhandler = mysql_connect("localhost", "writer_user", "password", "mta_users") -- Start with a read-write username
     if (not myhandler) then
     if (not myhandler) then

Latest revision as of 16:43, 11 February 2015


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

Changes the current MySQL session authentication.

Syntax

bool mysql_change_user ( MySQLConnection handler, string new_username, string new_password [, string new_database ] )

Required arguments

  • handler: A valid MySQL link
  • new_username: The new username
  • new_password: The new username password

Optional arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • new_database: Start the new session using a new default database

Returns

It it succeeds returns true, in other case returns false

Example

Example 1:

function resourceStart ( res )
  if (res == getThisResource()) then
    myhandler = mysql_connect("localhost", "writer_user", "password", "mta_users") -- Start with a read-write username
    if (not myhandler) then
      outputDebugString("Unable to connect to the database: (" .. mysql_errno(handler) .. ") " .. mysql_error(handler))
    else
      -- Apply some changes to the database here
      if (not mysql_change_user(myhandler, "localhost", "reader_user", "password", "mta_users")) then -- Change to a read-only user
        outputDebugString("Unable to set the database read-only user: (" ..
                           mysql_errno(handler) .. ") " .. mysql_error(handler))
        mysql_close(myhandler) -- Close the MySQL connection
      end
    end
  end
end

addEventHandler("onResourceStart", getRootElement(), resourceStart)

See also