MySQL
Server Functions
Name | Description |
---|---|
Connect | Connects to the MySQL server. |
Close | Closes the MySQL connection. |
Execute | Executes a SQL statement. |
Query | Executes a SQL statement and retrieve data. |
Connect
Connects to the MySQL server.
MySQL.Connect(string host, int port, string user, string password, string database)
info
The entire MySQL API is based on the X DevAPI. A MySQL server with X plugin is therefore required.
Example:
- Lua
- Squirrel
MySQL.Connect("localhost", 33060, "root", "mypw", "mydb")
MySQL.Connect("localhost", 33060, "root", "mypw", "mydb");
Close
Closes the MySQL connection.
MySQL.Close()
Execute
Executes a SQL statement.
MySQL.Execute(string statement, list params)
warning
The execution of the MySQL statement is asynchronous.
Example:
- Lua
- Squirrel
-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/register" then
Events.CallRemote("registerPlayer", { Player.GetRockstarID() })
end
end)
-- in server script:
Events.Subscribe("registerPlayer", function(rid)
MySQL.Execute("INSERT INTO users (rockstarid, money, admin) VALUES (?, ?, ?)", { rid, 1000, 0 })
end, true)
// in client script:
Events.Subscribe("chatCommand", function(command) {
if (command == "/register") {
Events.CallRemote("registerPlayer", [ Player.GetRockstarID() ]);
}
});
// in server script:
Events.Subscribe("registerPlayer", function(rid) {
MySQL.Execute("INSERT INTO users (rockstarid, money, admin) VALUES (?, ?, ?)", [ rid, 1000, 0 ]);
}, true);
Query
Executes a SQL statement and retrieve data.
Synchronous (blocking):
list result, int affectedItems, int autoIncrementValue = MySQL.Query(string statement, list params)
Asynchronous (non-blocking):
MySQL.Query(string statement, list params, function callback)
Example:
- Lua
- Squirrel
-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/login" then
Events.CallRemote("loginPlayer", { Player.GetRockstarID() })
end
end)
-- in server script:
Events.Subscribe("loginPlayer", function(rid)
-- sync:
local result, rows, increment = MySQL.Query("SELECT * FROM users WHERE rockstarid = ?", { rid })
if #result > 0 then
Console.Log("Player exist (money: " .. result[1]["money"] .. ", admin: " .. result[1]["admin"] .. ")")
end
-- async:
MySQL.Query("SELECT * FROM users WHERE rockstarid = ?", { rid }, function(result, rows, increment)
if #result > 0 then
Console.Log("Player exist (money: " .. result[1]["money"] .. ", admin: " .. result[1]["admin"] .. ")")
end
end)
end, true)
// in client script:
Events.Subscribe("chatCommand", function(command) {
if (command == "/login") {
Events.CallRemote("loginPlayer", [ Player.GetRockstarID() ]);
}
});
// in server script:
Events.Subscribe("loginPlayer", function(rid) {
// sync:
local result = MySQL.Query("SELECT * FROM users WHERE rockstarid = ?", [ rid ]);
// result[0] = data, result[1] = rows, result[2] = increment
if (result[0].len() > 0) {
Console.Log("Player exist (money: " + result[0][0]["money"] + ", admin: " + result[0][0]["admin"] + ")");
}
// async:
MySQL.Query("SELECT * FROM users WHERE rockstarid = ?", [ rid ], function(result, rows, increment) {
if (result.len() > 0) {
Console.Log("Player exist (money: " + result[0]["money"] + ", admin: " + result[0]["admin"] + ")");
}
});
}, true);