Skip to content

Instantly share code, notes, and snippets.

@Vince0789
Created July 8, 2021 16:45
Show Gist options
  • Save Vince0789/e7982372320b03819b72d3536627e880 to your computer and use it in GitHub Desktop.
Save Vince0789/e7982372320b03819b72d3536627e880 to your computer and use it in GitHub Desktop.
SA-MP MySQL Connect
/**
* <summary>
* Gets the current database handle. This method acts like a singleton; if a
* connection doesn't exist, one is created.
* </summary>
* <returns>
* MySQL database handle.
* </returns>
*/
stock MySQL:GetDatabaseHandle()
{
new MySQL:handle = MySQL:getproperty(.name = "gDatabaseHandle");
if(handle == MYSQL_INVALID_HANDLE)
{
handle = mysql_connect_file();
setproperty(.name = "gDatabaseHandle", .value = _:handle);
if(mysql_errno(handle) == 0)
{
mysql_set_charset("utf8mb4", handle);
mysql_tquery(handle, "SET @@SESSION.time_zone = '+00:00';");
mysql_tquery(handle, "\
SELECT CONCAT_WS(' ', 'Your MySQL connection id is', CONNECTION_ID()) \
UNION \
SELECT CONCAT_WS(' ', 'Server version:', @@version, @@version_comment);",
"OnVersionInformationAvailable", "d", _:handle
);
}
}
return handle;
}
/**
* <summary>
* Gets called when a new database handle is created. Prints version information.
* </summary>
*/
public OnVersionInformationAvailable(MySQL:handle)
{
new versionText[64];
cache_get_value_index(0, 0, versionText);
printf("\n[info] %s", versionText);
cache_get_value_index(1, 0, versionText);
printf("[info] %s\n", versionText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment