Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- D3A MySQL Controls
- require("mysql")
- local ip = "localhost"
- local user = "root"
- local pass = "********"
- local db = "d3a 4"
- D3A.MySQL.Connection = nil
- local function WriteError(e, q)
- local fn = "D3A/Logs/MySQLError.txt"
- local entr
- if (!q) then
- entr = Format("Time of error: %s\n%s\n\n", os.date(), e)
- else
- entr = Format("Time of error: %s\nQuery: %s\n%s\n\n", os.date(), q, e)
- end
- if (file.Exists(fn)) then
- file.Write(fn, entr .. file.Read(fn))
- else
- file.Write(fn, entr)
- end
- end
- function D3A.MySQL.Query(q)
- if (!D3A.MySQL.Connection) then
- print("D3A MYSQL :: Connecting to database")
- local db, error = mysql.connect(ip, user, pass, db)
- if (db == 0) then
- print("D3A MYSQL :: " .. error)
- WriteError(error)
- D3A.MySQL.Connection = false
- else
- print("D3A MYSQL :: Database connection successful")
- D3A.MySQL.Connection = db
- timer.Create("D3A.MySQL.TimeOut", 45, 1, function()
- if (D3A.MySQL.Connection) then
- print("D3A MYSQL :: Killing the database connection (inactivity)")
- mysql.disconnect(D3A.MySQL.Connection)
- D3A.MySQL.Connection = nil
- end
- end)
- end
- end
- if (!q) then
- print("D3A MYSQL :: Cannot run query - No query given.")
- return {}
- end
- if (D3A.MySQL.Connection) then
- local res, success, error = mysql.query(D3A.MySQL.Connection, mysql.escape(D3A.MySQL.Connection, q))
- if (!success) then
- print("D3A MYSQL :: " .. error)
- print("Given query: " .. q)
- WriteError(error, q)
- return {}
- end
- return res
- else
- print("D3A MYSQL :: Cannot run query - No database connection.")
- print("Given query: " .. q)
- return {}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement