Repository navigation
Prepared Statements
A prepared statement is sent to the server once and then run with values that are never escaped or parsed again. Its text takes the specifiers of queries, each one a value.
new
MySQL:gDatabase,
Statement:gSaveScore,
gPlayerId[MAX_PLAYERS]
;
stock PrepareStatements() {
gSaveScore = mysql_stmt_prepare(gDatabase, "UPDATE players SET score = %i WHERE id = %i");
}
stock SaveScore(playerid) {
mysql_stmt_execute(gSaveScore, "", "", GetPlayerScore(playerid), gPlayerId[playerid]);
}
stock SaveScores() {
new const
Batch:batch = mysql_batch_create(gSaveScore)
;
for (new i; i != MAX_PLAYERS; ++i) {
if (IsPlayerConnected(i)) {
mysql_batch_add_row(batch, GetPlayerScore(i), gPlayerId[i]);
}
}
mysql_batch_execute(batch);
}mysql_stmt_execute runs it once, the values of the public first and those of the statement after, and mysql_stmt_query waits for it. A statement runs on the connection of its handle that runs ordered queries, and is prepared again when the connection is lost or the tables change.
mysql_batch_create starts a batch, mysql_batch_add_row copies a row of values into it, and mysql_batch_execute sends them. On MariaDB 10.2 or later the rows go to the server as arrays in one command, split to fit max_allowed_packet, and on other servers they run one by one. A batch sent in more than one command runs in a transaction of its own, unless one is open already.
mysql 0.6.0 · Repository · Releases · MIT License
Guides
- Installation
- Configuration
- Queries
- The cache
- JSON cells
- Prepared statements
- Transactions
- The ORM
- Passwords
- Errors
- TLS
- Coming from another plugin
- Performance
Reference
Functions
Connections
Queries
Cache
- cache_get_row_count
- cache_get_field_count
- cache_get_result_count
- cache_get_field_name
- cache_get_field_type
- cache_get_field_index
- cache_set_result
- cache_get_value_index
- cache_get_value_index_int
- cache_get_value_index_float
- cache_get_value_index_bool
- cache_is_value_index_null
- cache_get_value_name
- cache_get_value_name_int
- cache_get_value_name_float
- cache_get_value_name_bool
- cache_is_value_name_null
- cache_get_row
- cache_save
- cache_delete
- cache_set_active
- cache_unset_active
- cache_is_any_active
- cache_is_valid
- cache_affected_rows
- cache_insert_id
- cache_warning_count
- cache_get_query_exec_time
- cache_get_query_string
- cache_get_errno