DbConnect: Difference between revisions
Jump to navigation
Jump to search
Complete Lua function catalog |
Release 0.1.4 BUILD133 / Protocol 36 and document authenticated event origin |
||
| (One intermediate revision by the same user not shown) | |||
| Line 48: | Line 48: | ||
[[Category:Server Functions]] | [[Category:Server Functions]] | ||
[[Category:Database Functions]] | [[Category:Database Functions]] | ||
== Recovery from 0.1.4 == | |||
'''Since 0.1.4.''' See [[MySQL connection recovery]], [[dbReconnect]], [[dbGetConnectionState]] and [[onDatabaseConnectionStateChange]]. Set the optional fifth argument to <code>charset=utf8mb4;autoreconnect=true</code> to enable background recovery after a successful initial connection. Default false preserves older scripts. Failed SQL is never replayed and transaction/session loss requires explicit recovery. | |||
Latest revision as of 20:40, 21 September 2026
dbConnect
Opens a server-side MySQL connection managed by the current resource.
Syntax
DatabaseConnection dbConnect(string driver, string connectionString, string username, string password [, string options = ""])
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
driver |
string |
yes | Database driver name. Currently only mysql is supported.
|
connectionString |
string |
yes | Semicolon-separated connection options such as host, port and database. |
username |
string |
yes | The database account name. |
password |
string |
yes | The database password or configured secret reference. |
options |
string |
no | Optional connection behavior flags. |
Returns
Returns a DatabaseConnection handle on success, or false on failure.
Examples
Connect to a configured MySQL database:
local db = dbConnect(
"mysql",
"host=127.0.0.1;port=3306;dbname=g1r_mp",
"g1r_server",
"env:G1R_DB_PASSWORD"
)
if not db then
outputDebugString("Database connection failed", 1)
end
Notes
- Available only in server-side resource scripts.
- Database access is server-side only. Never put database credentials in client resources.
Recovery from 0.1.4
Since 0.1.4. See MySQL connection recovery, dbReconnect, dbGetConnectionState and onDatabaseConnectionStateChange. Set the optional fifth argument to charset=utf8mb4;autoreconnect=true to enable background recovery after a successful initial connection. Default false preserves older scripts. Failed SQL is never replayed and transaction/session loss requires explicit recovery.