MySQL Plugin
From SA-MP Wiki
MySQL Plugin Documentation For SA:MP MySQL Plugin Version 2.0 By StrickenKid
Plugin Version 2.0 Has Been Released.
Plugin Callbacks
OnMysqlQuery
Description:
resultid | The result id passed from the mysql_query function. |
spareid | An extra variable passed from the mysql_query function. |
MySQL:handle | The mysql handle the query was executed on. |
This callback does not handle returns.
#define MYSQL_RESULT_LOGIN 1 #define MYSQL_RESULT_REGISTER 2 // in a login command mysql_query("SELECT * FROM `accounts` WHERE `username` = 'MyCoolNick' LIMIT 0,1", MYSQL_RESULT_LOGIN, playerid, connection); // in a register command mysql_query("INSERT INTO `accounts` (username, password) VALUES ('MyCoolNick', 'MyCoolPassword')", MYSQL_RESULT_REGISTER, playerid, connection); public OnMysqlQuery(resultid, spareid, MySQL:handle) { switch (resultid) { case MYSQL_RESULT_LOGIN: { // process login with spareid as playerid } case MYSQL_RESULT_REGISTER: { // process register with spareid as playerid } } return 1; }
OnMysqlQueryArray
Description:
resultid | The result id passed from the mysql_query_array function. |
extravars[] | An array of extra variables passed from the mysql_query_array function. |
MySQL:handle | The mysql handle the query was executed on. |
This callback does not handle returns.
#define MYSQL_RESULT_LOGIN 1 #define MYSQL_RESULT_REGISTER 2 // in a login command mysql_query_array("SELECT * FROM `accounts` WHERE `username` = 'MyCoolNick' LIMIT 0,1", MYSQL_RESULT_LOGIN, {playerid, someotherid, anotherid}, connection); // in a register command mysql_query("INSERT INTO `accounts` (username, password) VALUES ('MyCoolNick', 'MyCoolPassword')", MYSQL_RESULT_REGISTER, {playerid, someotherid, anotherid}, connection); public OnMysqlQueryArray(resultid, extravars[], MySQL:handle) { switch (resultid) { case MYSQL_RESULT_LOGIN: { // process login with: // extravars[0] = playerid // extravars[1] = someotherid // extravars[2] = anotherid } case MYSQL_RESULT_REGISTER: { // process register with: // extravars[0] = playerid // extravars[1] = someotherid // extravars[2] = anotherid } } return 1; }
OnMysqlError
Description:
error[] | The error dump string explaining the error. |
errorid | The error code of the error. |
MySQL:handle | The mysql handle the error happened on. |
This callback does not handle returns.
// lets say we're trying to connect to a mysql server that is not online new MySQL:connection = mysql_init(LOG_ONLY_ERRORS, 1); mysql_connect("1.2.3.4", "user", "pass", "dbname", connection); public OnMysqlError(error[], errorid, MySQL:handle) { // error[] = "Failed to connect. Can't connect to MySQL server on '1.2.3.4' (10060)." // errorid = 2003 }
Plugin Functions
Note | Every function (except mysql_init) has a MySQL handle parameter, the examples use this, but it is not required. The handle defaults at 0 if you choose not to use it. |
mysql_init
Description:
logtype | The logging type for the mysql connection. |
printerrors | Toggle whether you want mysql errors to be printed to the server window. |
Return Values:
public OnGameModeInit( ) { new MySQL:connection = mysql_init(LOG_ONLY_ERRORS, 1); // ... }
mysql_connect
Description:
const host[] | The address of the mysql database. |
const user[] | The username for the account you want to connect with. |
const pass[] | The password for the account you want to connect with. |
const db[] | The database you want to connect to within the mysql server. (This can be changed with mysql_select_db). |
MySQL:handle | The connection handle this will be preformed on. |
auto_reconnect | Optional; Toggle for auto reconnect on connection lost, happens after mysql_ping is called. (default is off). |
Return Values:
public OnGameModeInit( ) { new MySQL:connection = mysql_init(LOG_ONLY_ERRORS, 1); mysql_connect("hostname", "username", "password", "database", connection, 1); // ... }
mysql_close
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
public OnGameModeExit( ) { mysql_close(connection); }
mysql_select_db
Description:
const db[] | The name of the database you want to connect to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
public OnGameModeInit() { new MySQL:connection = mysql_init(LOG_ONLY_ERRORS, 1); mysql_connect("hostname", "username", "password", "myfirstdatabase", connection); new sql = mysql_query(...); // Will be executed on myfirstdatabase mysql_select_db("myseconddatabase"); sql = mysql_query(...); // Will be executed on myseconddatabase } //Example by Luka P.
mysql_query
Description:
const query[] | The query you want to execute. |
resultid | Optional; (This will cause your query to be executed in a separate thread.) The id of the result to be passed into the query callback. |
spareid | Optional; An extra variable that would be passed to the query callback. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_query_array
Description:
const query[] | The query you want to execute. |
resultid | Optional; (This will cause your query to be executed in a separate thread.) The id of the result to be passed into the query callback. |
{Float,_}:extravars[] | An array of extra variables that would be passed to the query callback. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_store_result
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_free_result
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_result_stored
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
if(mysql_result_stored()) print("The result is stored"); else print("The result isn't stored");
mysql_fetch_field
Description:
const fieldname[] | The name of the field you want to fetch. |
dest[] | The destenation string the field will be stored to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_fetch_field_num
Description:
fieldnum | The number index of the field you want to fetch. |
dest[] | The destenation string the field will be stored to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_fetch_row
Description:
dest[] | The destination string the row will be stored to. |
const splitter[] | The splitter you want to seperate each field in the row with (default:" |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_real_escape_string
Description:
const string[] | The string you want to be escaped. |
dest[] | The destination string the new escaped string will be stored to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_num_rows
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_num_fields
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_affected_rows
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_insert_id
Description:
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_ping
Description:
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_error
Description:
dest[] | The destenation string the error will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
if(!mysql_ping()) { print("MYSQL: Connection is alive!"); return 1; } else { print("MYSQL: Connection is dead."); return 1; }
mysql_errno
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_warning_count
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_info
Description:
dest[] | The destenation string the information will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_stat
Description:
dest[] | The destenation string the information will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_get_server_info
Description:
dest[] | The destenation string the information will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_get_host_info
Description:
dest[] | The destination string the information will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_data_seek
Description:
rownum | The row number you want to seek to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_set_character_set
Description:
const csname[] | The name of the character set you want to use. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_get_character_set
Description:
csname[] | The destination string the character set name will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_fetch_int
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_fetch_float
Description:
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
mysql_fetch_string
Description:
dest[] | The destination string the result will be saved to. |
MySQL:handle | The connection handle this will be preformed on. |
Return Values:
No example yet...
Others
Logging Types
Note: Logging logs to mysql_log.txt located in server directory.
LOG_OFF Log nothing LOG_ALL Log everything LOG_ONLY_ERRORS Log only errors
--StrickenKid 01:59, 4 August 2010 (CEST)