Skip to content

MySQL_CreateQuery

imshooter edited this page Oct 24, 2026 · 1 revision

Start a query to build a piece at a time.

native Query:MySQL_CreateQuery(MySQL:handle, const text[] = "", MYSQL_TAGS:...);

Parameters

Parameter Description Default
MySQL:handle Handle to send it through.
const text[] First piece, formatted with the values after it. ""
MYSQL_TAGS:... Values for the specifiers of the query, in order.

Returns

  • The query.
  • INVALID_QUERY_ID when the handle does not exist or the format is not valid.

Remarks

A query built in pieces lives in the component, so it has no length limit and no array. It belongs to the script, and is forgotten when sent, destroyed, or when the script unloads. Its text can go into another query with %Q, so a statement made of parts that grow together keeps each part in a query of its own. See Queries.

Example

Inserts every player in one query.

new MySQL:gDatabase;

stock SaveEveryone() {
    new
        name[MAX_PLAYER_NAME + 1],
        rows,
        Query:query = MySQL_CreateQuery(gDatabase, "INSERT INTO sessions (name, score) VALUES ")
    ;

    for (new i; i != MAX_PLAYERS; ++i) {
        if (!IsPlayerConnected(i)) {
            continue;
        }

        GetPlayerName(i, name, sizeof (name));
        MySQL_AppendQuery(query, "(%q, %i), ", name, GetPlayerScore(i));
        ++rows;
    }

    if (rows == 0) {
        MySQL_DestroyQuery(query);

        return;
    }

    MySQL_TrimQuery(query, 2);
    MySQL_SendQuery(query);
}

Related

Clone this wiki locally