Skip to main content

MySQL

Server Functions

NameDescription
ConnectConnects to the MySQL server.
CloseCloses the MySQL connection.
ExecuteExecutes a SQL statement.
QueryExecutes 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:

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:

-- 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)

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:

-- 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)