From the reference libraryFiveM

Use your preferred format. Make sure you only ever use set.

ox/oxmysql/index.mdx

Use your preferred format. Make sure you only ever use set.

set mysql_connection_string "mysql://root:12345@localhost:3306/fivem" set mysql_connection_string "user=root;password=12345;host=localhost;port=3306;database=fivem"


Certain special characters are reserved or unsupported depending on your connection string.  
Avoid using these characters `; , / ? : @ & = + $ #`, and try swapping connection string format.

### Slow query warnings [step]

You will receive warnings if a query took a long time to complete, configurable with a convar.

- Query time may not be entirely accurate.
- Slow queries may not indicate a database issue (e.g. server hitches).
- Slow queries on server startup are not necessarily problematic.

```bash
set mysql_slow_query_warning 150

Debug [step]

Enabling the debug option will print all queries in the server console; you can also use an array to only print from a list of resources instead.

set mysql_debug true
set mysql_debug [
  "ox_core",
  "ox_inventory"
]

You can temporarily modify the resource list with commands.

oxmysql_debug remove ox_core
oxmysql_debug add ox_core

Compatibility

You can delete the following resources and allow oxmysql to provide their functionality.

  • mysql-async
  • ghmattimysql

Usage

Resources can import oxmysql methods by including our library, granting some type-checking and minor performance improvements over raw export calls.

Lua

Modify fxmanifest.lua for your resource, and add the following above any other script files.

server_script '@oxmysql/lib/MySQL.lua'

JavaScript

You can use raw exports, or install our npm package for intellisense and similar usage as Lua.

# With pnpm
pnpm add @overextended/oxmysql

# With Yarn
yarn add @overextended/oxmysql

# With npm
npm install @overextended/oxmysql

Import the oxmysql object into your resource.

import { oxmysql as MySQL } from "@overextended/oxmysql";

Upserting

When uncertain if a row should be inserted into the database, or an existing row should be updated, queries should check for duplicate keys.

MySQL.prepare('INSERT INTO ox_inventory (owner, name, data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)', { owner, dbId, inventory })

This is preferred over checking the existence of a row, then inserting or updating depending on the result.
Furthermore, unlike using 'REPLACE INTO', the row is not deleted and re-inserted.

Back to documentation