Skip to content

Queries

imshooter edited this page Nov 29, 2026 · 1 revision

In order

mysql_tquery runs a query after the queries of its handle sent before it, on one connection, and calls a public with the values of its format when it is done. mysql_tquery_format also formats the query in the plugin, with no length limit, the values of the public first and those of the query after.

new MySQL:gDatabase;

forward OnBanChecked(playerid);

stock CheckBan(playerid, const name[]) {
    mysql_tquery_format(gDatabase, "SELECT reason FROM bans WHERE name = %q", "OnBanChecked", "i", playerid, name);
}

public OnBanChecked(playerid) {
    if (cache_num_rows() != 0) {
        Kick(playerid);
    }

    return 1;
}

mysql_send formats and sends a query that needs no answer.

In parallel

mysql_pquery and mysql_pquery_format hand the query to any connection of the pool, so it may finish before queries sent earlier. Without a pool they run in order.

Waited for

mysql_query and mysql_query_format run the query on the game thread and wait for it. They suit the tables made at start up. A kept result is the active cache until another is chosen, and stays until cache_delete.

new MySQL:gDatabase;

stock MakeTables() {
    mysql_query(gDatabase, "CREATE TABLE IF NOT EXISTS players (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(24) NOT NULL UNIQUE, score INT NOT NULL DEFAULT 0)", false);
}

Built in pieces

A query too long for one call is built with mysql_builder_create, mysql_builder_append and mysql_builder_trim, then sent with mysql_builder_tquery. %Q writes the text of a builder into another.

new MySQL:gDatabase;

stock SaveScores() {
    new const
        Builder:builder = mysql_builder_create(gDatabase, "INSERT INTO scores (player, score) VALUES ")
    ;

    for (new i; i != MAX_PLAYERS; ++i) {
        if (IsPlayerConnected(i)) {
            mysql_builder_append(builder, "(%i, %i),", i, GetPlayerScore(i));
        }
    }

    mysql_builder_trim(builder, 1);
    mysql_builder_tquery(builder);
}

From a file

mysql_tquery_file and mysql_query_file run the queries of a file in scriptfiles, split at semicolons outside quotes and comments. They stop at the first that fails, and each gives a result of the cache.

Formatting into an array

mysql_format writes a formatted query into an array, and mysql_escape_string escapes one string. See Specifiers.

Home

Guides

Reference

Functions

Connections
Queries
Cache
JSON cells
ORM
Queries built in pieces
Prepared statements
Batches
Transactions
Passwords
Callbacks

Clone this wiki locally