Modules/MTA-MySQL/mysql connect: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: Makes a connection to a MySQL server and returns a handler to it. ==Syntax== <syntaxhighlight lang="lua"> handler mysql_connect ( string hostname, string username, string password, string database, [ in...)
 
mNo edit summary
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<pageclass class="#AA7592" subcaption="MTA-MySQL Module"></pageclass>
__NOTOC__
{{ModuleFunction|MTA-MySQL}}
Makes a connection to a MySQL server and returns a handler to it.
Makes a connection to a MySQL server and returns a handler to it.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
handler mysql_connect ( string hostname, string username, string password, string database, [ int port=3306, string unix_socket=nil, string client_flags=""] )
MySQLConnection mysql_connect ( string hostname, string username, string password, string database, [ int port=3306, string unix_socket=nil, string client_flags=""] )
</syntaxhighlight>
</syntaxhighlight>
===Required arguments===
===Required arguments===
Line 35: Line 38:
else
else
   mysql_close(handler) -- Close the connection
   mysql_close(handler) -- Close the connection
end
</syntaxhighlight>
'''Example 2:''' This example makes a MySQL connection and set charset
<syntaxhighlight lang="lua">
local charset = "utf8";
local handler = mysql_connect("127.0.0.1", "username", "password", "database", 3306, "/var/lib/libmysqlclient.so.15", "")
if (handler) then
mysql_query(handler, "SET NAMES '"..charset.."'")
mysql_query(handler, "SET OPTION CHARACTER SET "..charset.."")
mysql_query(handler, "SET CHARACTER SET '"..charset.."'")
end
end
</syntaxhighlight>
</syntaxhighlight>
Line 40: Line 54:
==See also==
==See also==
{{Modules/MTA-MySQL/Handler_functions}}
{{Modules/MTA-MySQL/Handler_functions}}
[[RU:Modules/MTA-MySQL/mysql_connect]]

Latest revision as of 10:37, 2 December 2017


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

Makes a connection to a MySQL server and returns a handler to it.

Syntax

MySQLConnection mysql_connect ( string hostname, string username, string password, string database, [ int port=3306, string unix_socket=nil, string client_flags=""] )

Required arguments

  • hostname: The hostname to connect to
  • username: The database username
  • password: The username password
  • database: The starting database

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.

  • port: The MySQL port
  • unix_socket: When connecting in unix systems to a local database, the path for the unix socket (usually /var/run/mysqld/mysqld.sock)
  • client_flags: The connection flags, with the format "flag1|flag2|flag3". The list of flags is:
    • compress: Use compression protocol.
    • found_rows: Return the number of found (matched) rows, not the number of changed rows.
    • ignore_sigpipe: Prevents the client library from installing a SIGPIPE signal handler. This can be used to avoid conflicts with a handler that the application has already installed.
    • ignore_space: Allow spaces after function names. Makes all functions names reserved words.
    • interactive: Allow interactive_timeout seconds (instead of wait_timeout seconds) of inactivity before closing the connection. The client's session wait_timeout variable is set to the value of the session interactive_timeout variable.
    • local_files: Enable LOAD DATA LOCAL handling.
    • no_schema: Don't allow the db_name.tbl_name.col_name syntax. This is for ODBC. It causes the parser to generate an error if you use that syntax, which is useful for trapping bugs in some ODBC programs.

Returns

A valid MySQL handler if it was able to connect, or nil if it was unable.

Example

Example 1: This example makes a MySQL connection and checks if it was succesful.

handler = mysql_connect("localhost", "username", "password", "mta_users") -- Establish the connection
if ( not handler ) then -- The connection failed
  outputDebugString("Unable to connect to the MySQL server")
else
  mysql_close(handler) -- Close the connection
end

Example 2: This example makes a MySQL connection and set charset

local charset = "utf8";
local handler = mysql_connect("127.0.0.1", "username", "password", "database", 3306, "/var/lib/libmysqlclient.so.15", "")

if (handler) then
	mysql_query(handler, "SET NAMES '"..charset.."'")
	mysql_query(handler, "SET OPTION CHARACTER SET "..charset.."")
	mysql_query(handler, "SET CHARACTER SET '"..charset.."'")
end

See also