Skip to content

MySQL_Prepare

imshooter edited this page Oct 24, 2026 · 1 revision

Prepare a statement, with a specifier for each value.

It is sent to the server at once and runs on the connection of the handle that runs queries in order. An error in it is logged, and reported through OnMySQLError, before it first runs.

native Statement:MySQL_Prepare(MySQL:handle, const query[]);

Parameters

Parameter Description
MySQL:handle Handle to run it through.
const query[] The statement, as INSERT INTO items (uuid, slot) VALUES (%q, %i).

Returns

  • The statement.
  • INVALID_STATEMENT_ID when the handle does not exist or a specifier is not valid.

Remarks

Each specifier becomes a ? the server fills with a value, so a value is never escaped and never parsed as SQL. %i, %d and %x take an integer, %u an unsigned one, %f a float, %s, %e, %q and %j a string, and %Q the text of a query being built. %% is a percent sign. A ? written in the text is a placeholder of its own, which makes the statement fail. See Prepared statements.

A statement belongs to the script and runs on the connection of the handle that runs queries in order. It is prepared again when that connection is lost, or when the server asks for it after a table changed.

Example

new
    MySQL:gDatabase,
    Statement:gSaveScore
;

public OnGameModeInit() {
    gDatabase = MySQL_ConnectFile("mysql.ini");
    gSaveScore = MySQL_Prepare(gDatabase, "UPDATE players SET score = %i WHERE id = %i");

    return 1;
}

stock SaveScore(playerid, accountid) {
    MySQL_Execute(gSaveScore, "", 0, GetPlayerScore(playerid), accountid);
}

Related

Clone this wiki locally