fivemoxmysqldatabasemysqllua
oxmysql คู่มือใช้งาน — Database สำหรับ FiveM ยุคใหม่
March 22, 2026
2 min read
183 views
oxmysql คู่มือใช้งาน
oxmysql คือ MySQL resource สำหรับ FiveM ที่ออกแบบมาให้เร็วและ modern กว่า mysql-async แบบเก่า
ทำไมต้องใช้ oxmysql?
- เร็วกว่า mysql-async 2-3x
- รองรับ async/await pattern
- Promise-based API
- Prepared statements ป้องกัน SQL injection
- รองรับ transactions
การติดตั้ง
ensure oxmysql
ใน fxmanifest.lua:
server_scripts {
'@oxmysql/lib/MySQL.lua',
'server.lua'
}
API Methods
MySQL.query — query ทั่วไป
-- Async (callback)
MySQL.query('SELECT * FROM users WHERE identifier = ?',
{ identifier },
function(result)
print(json.encode(result))
end
)
-- Sync (หยุดรอ)
local result = MySQL.query.await('SELECT * FROM users WHERE identifier = ?',
{ identifier }
)
MySQL.single — ดึง row เดียว
local user = MySQL.single.await(
'SELECT * FROM users WHERE identifier = ? LIMIT 1',
{ identifier }
)
if user then
print('Found:', user.name)
end
MySQL.scalar — ดึงค่าเดียว
local count = MySQL.scalar.await(
'SELECT COUNT(*) FROM users WHERE job = ?',
{ 'police' }
)
print('Police count:', count)
MySQL.insert — INSERT และรับ ID กลับ
local insertId = MySQL.insert.await(
'INSERT INTO custom_data (identifier, data) VALUES (?, ?)',
{ identifier, json.encode(data) }
)
print('New ID:', insertId)
MySQL.update — UPDATE และรับ rows affected
local affected = MySQL.update.await(
'UPDATE users SET money = money + ? WHERE identifier = ?',
{ amount, identifier }
)
if affected > 0 then
print('Updated successfully')
end
MySQL.transaction — หลาย queries ใน transaction เดียว
local success = MySQL.transaction.await({
{ query = 'UPDATE accounts SET balance = balance - ? WHERE identifier = ?', values = { amount, fromId } },
{ query = 'UPDATE accounts SET balance = balance + ? WHERE identifier = ?', values = { amount, toId } }
})
if success then
print('Transfer complete')
else
print('Transfer failed — rolled back')
end
Real-World Pattern
-- สร้าง character ใหม่
local function CreateCharacter(identifier, name, dob)
-- ตรวจสอบว่ามีอยู่แล้วไหม
local existing = MySQL.single.await(
'SELECT id FROM characters WHERE identifier = ?',
{ identifier }
)
if existing then
return false, 'Character already exists'
end
local id = MySQL.insert.await(
'INSERT INTO characters (identifier, name, dob, created_at) VALUES (?, ?, ?, NOW())',
{ identifier, name, dob }
)
return true, id
end
Performance Tips
- ใช้
.awaitเฉพาะตอนจำเป็น — callback ยังเร็วกว่าในบางกรณี - Index columns ที่ใช้ใน WHERE เสมอ
- ใช้
MySQL.scalarแทนMySQL.queryเมื่อต้องการค่าเดียว - Transaction สำหรับ operations ที่ต้อง atomic
สรุป
oxmysql เป็น upgrade ที่คุ้มค่ามากจาก mysql-async API ที่ clean กว่า เร็วกว่า และ error handling ดีกว่า ควรย้ายมาใช้ถ้ายังไม่ได้ย้าย
Related Articles
Breaking: GTA Online อัพเดท "Money Fronts" — FiveM Server Owners ต้องทำอะไร?
Rockstar ปล่อย GTA Online อัพเดท Money Fronts มีผลกระทบต่อ FiveM servers บางส่วน นี่คือสิ่งที่ต้องทำทันทีหลังอัพเดท
Community Spotlight: Script และ Projects ที่น่าสนใจจาก FiveM Community
รวม scripts, tools และ projects ที่โดดเด่นจาก FiveM community ในช่วงที่ผ่านมา ตั้งแต่ free resources ถึง open-source projects
txAdmin อัพเดทใหม่ — Dashboard, Diagnostics และ Ban System ที่ดีขึ้น
txAdmin ซึ่งตอนนี้เป็นส่วนหนึ่งของ Cfx.re อย่างเป็นทางการ ได้รับการอัพเดทครั้งใหญ่ มี features ใหม่ที่ทำให้ Server Management ง่ายขึ้นมาก