oxmysql Queries: How to Read and Write from a FiveM Script
Read and write FiveM database data with oxmysql using MySQL.query, MySQL.single, MySQL.insert, MySQL.update, ? placeholders and await examples.
Short answer
Use MySQL.query or MySQL.single to read rows, and MySQL.insert or MySQL.update to write them. Put the SQL in a string with ? placeholders, and pass the values in a table as the second argument. Never join user input into the SQL string.
Prerequisites
- oxmysql is installed and started. Put
ensure oxmysqlinserver.cfgbefore any resource that uses it. - The connection string is set with
set, before resources start. The docs use the convarmysql_connection_string:
set mysql_connection_string "mysql://user:password@localhost:3306/database_name"
If the server cannot connect at all, see oxmysql ECONNREFUSED on port 3306. If you still need a database server, follow install MariaDB for FiveM step by step.
Reading data
MySQL.query returns all matching rows. MySQL.single returns one row, so add LIMIT 1 and check for nil:
local rows = MySQL.query.await('SELECT `column_a`, `column_b` FROM `table_name` WHERE `identifier` = ?', {
identifier
})
local row = MySQL.single.await('SELECT `column_a` FROM `table_name` WHERE `identifier` = ? LIMIT 1', {
identifier
})
if not row then return end
Writing data
MySQL.insert returns the new row's insert ID. MySQL.update returns the number of rows affected:
local newId = MySQL.insert.await('INSERT INTO `table_name` (`column_a`, `column_b`) VALUES (?, ?)', {
valueA, valueB
})
local affectedRows = MySQL.update.await('UPDATE `table_name` SET `column_a` = ? WHERE `identifier` = ?', {
newValue, identifier
})
The callback form takes the same arguments, with the parameters table before the callback:
MySQL.update('UPDATE `table_name` SET `column_a` = ? WHERE `identifier` = ?', {
newValue, identifier
}, function(affectedRows)
print(affectedRows)
end)
Parameters and why
The ? placeholders let oxmysql prepare and escape each value. The docs state that this "allows queries to be properly prepared and escaped", which protects against SQL injection and is faster than building the string yourself. Named placeholders are deprecated, so use ?.
Async versus sync
The docs describe three forms for each function: .await in Lua, a callback function, and a promise in JavaScript. The .await form pauses only the Lua thread that calls it, so the docs wrap it in CreateThread:
CreateThread(function()
local row = MySQL.single.await('SELECT `column_a` FROM `table_name` WHERE `identifier` = ? LIMIT 1', {
identifier
})
print(row and row.column_a)
end)
The callback form runs when the result arrives. The docs do not say whether a query blocks the server, so do not assume either way. Use whichever form fits your code, and do not mix them up within one function.
Common mistakes
- SQL injection: building the query with
..and player input lets a player change the SQL. Always use?and pass the value in the table. - Missing await or callback: without
.awaitor a callback, you get no rows. Your code then reads from an empty result or a promise, not the data. - Wrong table name: a typo in the table or column name makes the query fail. Match the names in your database exactly, and check the server console for the error.
FAQ
Should I use MySQL.query or MySQL.single?
Use MySQL.single when you expect one row, such as one player's record. Use MySQL.query when you need a list of rows.
Do I need CreateThread for .await?
Yes, in the docs' examples. Call .await inside a thread, or use the callback form instead.
Why is my result nil?
The query may have matched no rows, the table or column name may be wrong, or you read the result before it arrived. Check the query in your database client and add a nil check.
Can I use named placeholders?
The docs say named placeholders are deprecated and should be avoided, so use ? with a values table.
Sources
- OxMySQL documentation, Overextended
- MySQL.query
- MySQL.single
- MySQL.insert
- MySQL.update
- overextended/oxmysql on GitHub (v2.2.3)
Last checked: 30 November 2026
Note: the function names, placeholder rules and return values were checked against the oxmysql docs on this date. The docs do not say whether queries block the server thread, so this post does not claim either way.
FiveM.tools was founded with sponsorship from Nyru.