Kostenlose Gameserver für Minecraft, Terraria, CS & Discord · Ohne Kreditkarte!
SA-MP7 min•November 12, 2026

How to Connect SA:MP & open.mp to MySQL: Complete pBlueG R41-4 Tutorial

Step-by-step guide to modern asynchronous database management in SA:MP. Configure mysql_tquery, prevent main-thread lag during player logins, secure database credentials, and manage remote tables.

CoalByte Multiplayer Team
CoalByte Multiplayer Team
Database Systems Specialist
How to Connect SA:MP & open.mp to MySQL: Complete pBlueG R41-4 Tutorial

#Why Synchronous `mysql_query` Freezes Your Server

In early SA:MP gamemodes (using MySQL R6 or R7), queries were executed synchronously using mysql_query. This meant that when a player logged in, the entire server froze while waiting for the MySQL disk to read the player's inventory and password hash.

The modern industry standard is pBlueG MySQL R41-4, which executes queries asynchronously on a background worker thread via mysql_tquery.


1. Initializing the Database Connection

In your gamemode's OnGameModeInit:

pawn
#include <a_samp>
#include <a_mysql>

new MySQL:g_SQL;

public OnGameModeInit()
{
    new MySQLOpt:options = mysql_init_options();
    mysql_set_option(options, AUTO_RECONNECT, true);

    g_SQL = mysql_connect("127.0.0.1", "samp_user", "SecurePassword123!", "samp_database", options);

    if (mysql_errno(g_SQL) != 0) {
        print("❌ [MySQL] Connection failed! Halting server startup.");
        SendRconCommand("exit");
        return 0;
    }

    print("✅ [MySQL] Successfully connected to database pool.");
    return 1;
}

public OnGameModeExit()
{
    mysql_close(g_SQL);
    return 1;
}

2. Loading Player Accounts with `mysql_tquery`

pawn
public OnPlayerConnect(playerid)
{
    new query[128], playerName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, playerName, sizeof(playerName));

    mysql_format(g_SQL, query, sizeof(query), 
        "SELECT id, password, score, money FROM users WHERE username = '%e' LIMIT 1", 
        playerName
    );

    // Query runs in the background thread. Triggers OnAccountDataLoaded when ready.
    mysql_tquery(g_SQL, query, "OnAccountDataLoaded", "d", playerid);
    return 1;
}

forward OnAccountDataLoaded(playerid);
public OnAccountDataLoaded(playerid)
{
    if (!IsPlayerConnected(playerid)) return 1;

    new rows = cache_num_rows();
    if (rows > 0) {
        // Player exists -> prompt for password dialog
        ShowPlayerLoginDialog(playerid);
    } else {
        // Account does not exist -> prompt for registration
        ShowPlayerRegisterDialog(playerid);
    }
    return 1;
}

3. Preventing SQL Injection with `mysql_format` & `%e`

Never use standard format to build SQL queries! A user named admin' OR '1'='1 could bypass authentication. Always use mysql_format with the %e specifier to automatically escape dangerous characters:

pawn
// ✅ SECURE: Escapes malicious input automatically
mysql_format(g_SQL, query, sizeof(query), "SELECT * FROM users WHERE name = '%e'", inputtext);

4. Zero-Latency Hosting on CoalByte

Deploy your SA:MP or open.mp server with full SFTP access and MySQL support in seconds on CoalByte Free SA:MP Hosting.

Tags:#SA-MP#MySQL#pBlueG#open.mp#Pawn Database#SQL Optimization#Tutorial
⚡ 100% Free · No Credit Card Required

Ready for 24/7 high-performance free hosting?

Launch your dedicated server with 3 GB RAM, 2 vCPU, 8 GB fast SSD, and Anycast Anti-DDoS in under 60 seconds.

Deploy Free Server
Alle Artikel