Grzanek Posted October 28, 2021 Share Posted October 28, 2021 Hi, I have a problem with a script that connects a server to a database. Below I attach the script code and error content ERROR: nl_mysql\s.lua:98: attempt to index upvalue 'connection' (a boolean value) ERROR: nl_login\s.lua:24: call: failed to call 'TR_mysql:querry' [string "?"] Database = {} Database.__index = Database function Database:new(...) local instance = {} setmetatable(instance, Database) if instance:constructor(...) then return instance end return false end function Database:constructor(...) self.database = arg[1] self.host = arg[2] self.username = arg[3] self.password = arg[4] self.func = {} self.func.queryAsyncResponse = function(...) self:queryAsyncResponse(...) end self.func.queryAsyncMultiselectResponse = function(...) self:queryAsyncMultiselectResponse(...) end if self:connect() then return true else return false end end function Database:connect() setRuleValue("xddd", self.database) setRuleValue("localhost", self.host) setRuleValue("xddd", self.username) setRuleValue("xdddd", self.password) self.connection = dbConnect("mysql", string.format("dbname=%s;host=%s;unix_socket=/var/run/mysqld/mysqld.sock", self.database, self.host), self.username, self.password, "share=1;multi_statements=1") if self.connection then outputDebugString(string.format("[MYSQL] Connected to the database (%s)", self.database)) self:updateNames() return true else outputDebugString(string.format("[MYSQL] Cannot connect to the database (%s)", self.database), 3, 255, 0, 0) return false end end function Database:query(...) local qh = dbQuery(self.connection, dbPrepareString(self.connection, ...)) if not qh then return false end local result, num_affected_rows, last_insert_id = dbPoll(qh, -1, true) if not result then dbFree(qh) end return result, num_affected_rows, last_insert_id end function Database:queryAsync(data, ...) dbQuery(self.func.queryAsyncResponse, {{data}}, self.connection, dbPrepareString(self.connection, ...)) end function Database:queryAsyncWithoutResponse(...) dbQuery(function(qh) dbPoll(qh, 0, true) end, {}, self.connection, dbPrepareString(self.connection, ...)) end function Database:queryAsyncResponse(qh, data) local result, num_affected_rows, last_insert_id = dbPoll(qh, 0, true) triggerEvent(data[1].callback, resourceRoot, data[1], result, num_affected_rows, last_insert_id) end function Database:queryAsyncMultiselect(data, ...) dbQuery(self.func.queryAsyncMultiselectResponse, {{data}}, self.connection, dbPrepareString(self.connection, ...)) end function Database:queryAsyncMultiselectResponse(qh, data) local result, num_affected_rows, last_insert_id = dbPoll(qh, 0, true) triggerEvent(data[1].callback, resourceRoot, data[1], result, num_affected_rows, last_insert_id) end function Database:queryMultiselect(...) local qh = dbQuery(self.connection, dbPrepareString(self.connection, ...)) if not qh then return false end local result, num_affected_rows, last_insert_id = dbPoll(qh, -1, true) if not result then dbFree(qh) end return result, num_affected_rows, last_insert_id end function Database:updateNames() self:query("SET NAMES utf8") end local connection function createMysql() connection = Database:new("database", "host", "username", "password") end function query(...) local query, rows, lastID = connection:query(...) return query, rows, lastID end function queryMultiselect(...) local time = getTickCount() local query, rows, lastID = connection:queryMultiselect(...) addDevData(getResourceName(sourceResource), getTickCount() - time, #query) return query, rows, lastID end function queryAsync(callback, ...) connection:queryAsync(callback, ...) end function queryAsyncMultiselect(callback, ...) connection:queryAsyncMultiselect(callback, ...) end function queryAsyncWithoutResponse(...) connection:queryAsyncWithoutResponse(...) end -- Export to dev admin data function addDevData(resourceName, time, results) exports.TR_admin:addMysqlInfo(string.format("Resource: %s Time: %d tick Results: %d", resourceName, time, results)) end createMysql() Link to comment
The_GTA Posted October 28, 2021 Share Posted October 28, 2021 Hello Grzanek, have you tried putting outputDebugString to find out what is going wrong in your code? It seems like you are not expecting an error condition. Link to comment
Grzanek Posted October 28, 2021 Author Share Posted October 28, 2021 Problem occur in these lines function query(...) local query, rows, lastID = connection:query(...) return query, rows, lastID end Link to comment
The_GTA Posted October 28, 2021 Share Posted October 28, 2021 2 minutes ago, Grzanek said: Problem occur in these lines function query(...) local query, rows, lastID = connection:query(...) return query, rows, lastID end No, the problem has a different cause. Are you sure there is no other text inside the debug console? Link to comment
Grzanek Posted October 28, 2021 Author Share Posted October 28, 2021 https://imgur.com/a/GOGNWsq The script that references this script has errors in these lines local checkUsername = exports.nl_mysql:querry("SELECT UID FROM `tr_accounts` WHERE `login` = ? LIMIT 1", login) local checkEmail = exports.nl_mysql:querry("SELECT UID FROM `tr_accounts` WHERE `email` = ? LIMIT 1", email) local checkSerial = exports.nl_mysql:querry("SELECT UID FROM `tr_accounts` WHERE `serial` = ? LIMIT 2", serial) Link to comment
The_GTA Posted October 28, 2021 Share Posted October 28, 2021 (edited) 1 hour ago, Grzanek said: https://imgur.com/a/GOGNWsq Have you tried connecting to your server, logging in as admin and typing in "/debugscript 3"? This might give you more debug messages that you would otherwise miss. There are several potential errors in your script but you are not providing all the necessary details to analyze the problem. I am noticing a spelling mistake "querry" compared to "query" but I don't want to support this thread any further. I invite you to find somebody else to finish the resource(s) for you. Edited October 28, 2021 by The_GTA Link to comment
Grzanek Posted October 28, 2021 Author Share Posted October 28, 2021 The script has errors in these lines local checkUsername = exports.nl_mysql:querry("SELECT UID FROM `nl_accounts` WHERE `login` = ? LIMIT 1", login) local checkSerial = exports.nl_mysql:querry("SELECT UID FROM `nl_accounts` WHERE `serial` = ? LIMIT 2", serial) local checkEmail = exports.nl_mysql:querry("SELECT UID FROM `nl_accounts` WHERE `email` = ? LIMIT 1", email) https://imgur.com/SHna2gp Link to comment
Misha_Konsta Posted October 28, 2021 Share Posted October 28, 2021 (edited) Try to replace nl_mysql:querry to nl_mysql:query local checkUsername = exports.nl_mysql:query("SELECT UID FROM `nl_accounts` WHERE `login` = ? LIMIT 1", login) local checkSerial = exports.nl_mysql:query("SELECT UID FROM `nl_accounts` WHERE `serial` = ? LIMIT 2", serial) local checkEmail = exports.nl_mysql:query("SELECT UID FROM `nl_accounts` WHERE `email` = ? LIMIT 1", email) Edited October 28, 2021 by Misha_Konsta Link to comment
Moderators IIYAMA Posted November 4, 2021 Moderators Share Posted November 4, 2021 Locked for unclear ownership of (multiple) resource(s) which code is used on multiple topics. TR_mysql <or/and> nl_mysql Link to comment
Recommended Posts