ironimust Posted June 24, 2023 Share Posted June 24, 2023 Hello, my English may be bad. Anyway the problem is that the mysql script I use for my roleplay server is not working. mysql works fine on mta 1.5 server, but it does not work on mta 1.6 server, can you help me? function connectToDatabase(res) MySQLConnection = mysql_connect(hostname, username, password, database, port) -- *[*This is the line of code that throws the error*]* if (not MySQLConnection) then if (res == getThisResource()) then cancelEvent(true, "Cannot connect to the database.") end return nil end return nil end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), connectToDatabase, false) Link to comment
βurak Posted June 24, 2023 Share Posted June 24, 2023 I think the mysql_connect function may not work properly anymore because it belongs to the old mysql module, it is very old, now try using dbConnect Link to comment
ironimust Posted June 24, 2023 Author Share Posted June 24, 2023 1 hour ago, Burak5312 said: I think the mysql_connect function may not work properly anymore because it belongs to the old mysql module, it is very old, now try using dbConnect I don't know much. Can you make this code compatible with dbConnect or can you post a working mysql connection script? Link to comment
βurak Posted June 24, 2023 Share Posted June 24, 2023 (edited) 1 hour ago, ironimust said: I don't know much. Can you make this code compatible with dbConnect or can you post a working mysql connection script? can you show the code and also show the error you got let's see what we can do Edited June 24, 2023 by Burak5312 Link to comment
klue Posted June 25, 2023 Share Posted June 25, 2023 (edited) @ironimust local connection; local credentials = { dbname = "-", host = "-", port = "3306", charset = "utf8", username = "-", password = "-", multi_statements = "1" }; addEventHandler("onResourceStart", getResourceRootElement(), function () if isElement(connection) then print(" Error occured - Connection already exists!"); return false; end connection = dbConnect("mysql", "dbname=" .. credentials.dbname .. ";host=" .. credentials.host .. ";port=" .. credentials.port .. ";charset=" .. credentials.charset, credentials.username, credentials.password, "autoreconnect=1;multi_statements=" .. credentials.multi_statements); if not connection then print(" Error occured - Failed to connect!"); return false; else print(" Database connected! (" .. credentials.dbname .. ")"); end end); Edited June 25, 2023 by klue fix. Link to comment
ironimust Posted June 25, 2023 Author Share Posted June 25, 2023 14 hours ago, Burak5312 said: can you show the code and also show the error you got let's see what we can do MySQLConnection = mysql_connect(hostname, username, password, database, port) error code line. Link to comment
ironimust Posted June 25, 2023 Author Share Posted June 25, 2023 6 hours ago, klue said: @ironimust local connection; local credentials = { dbname = "-", host = "-", port = "3306", charset = "utf8", username = "-", password = "-", multi_statements = "1" }; addEventHandler("onResourceStart", getResourceRootElement(), function () if isElement(connection) then print(" Error occured - Connection already exists!"); return false; end connection = dbConnect("mysql", "dbname=" .. credentials.dbname .. ";host=" .. credentials.host .. ";port=" .. credentials.port .. ";charset=" .. credentials.charset, credentials.username, credentials.password, "autoreconnect=1;multi_statements=" .. credentials.multi_statements); if not connection then print(" Error occured - Failed to connect!"); return false; else print(" Database connected! (" .. credentials.dbname .. ")"); end end); I am trying. Link to comment
ironimust Posted June 25, 2023 Author Share Posted June 25, 2023 8 hours ago, klue said: @ironimust local connection; local credentials = { dbname = "-", host = "-", port = "3306", charset = "utf8", username = "-", password = "-", multi_statements = "1" }; addEventHandler("onResourceStart", getResourceRootElement(), function () if isElement(connection) then print(" Error occured - Connection already exists!"); return false; end connection = dbConnect("mysql", "dbname=" .. credentials.dbname .. ";host=" .. credentials.host .. ";port=" .. credentials.port .. ";charset=" .. credentials.charset, credentials.username, credentials.password, "autoreconnect=1;multi_statements=" .. credentials.multi_statements); if not connection then print(" Error occured - Failed to connect!"); return false; else print(" Database connected! (" .. credentials.dbname .. ")"); end end); not succesful Link to comment
ZeDni Posted June 25, 2023 Share Posted June 25, 2023 (edited) local hostname = "127.0.0.1" local username = "root" local password = "" local database = "Database Name" local port = "3306" local gMySQLCon = nil function connectToDatabase(res) gMySQLCon = dbConnect("mysql", "dbname=" .. database .. ";host=" .. hostname, username, password) if (not gMySQLCon) then cancelEvent(true, "[Error]: Database could not be connected!") return end outputServerLog("[Done]: Database connected!") end addEventHandler("onResourceStart", getResourceRootElement(), connectToDatabase, false) function getMySQLC() return gMySQLCon end Try This Code. @ironimust Edited June 25, 2023 by ZeDni Link to comment
ironimust Posted June 25, 2023 Author Share Posted June 25, 2023 5 minutes ago, ZeDni said: local hostname = "127.0.0.1" local username = "root" local password = "" local database = "Database Name" local port = "3306" local gMySQLCon = nil function connectToDatabase(res) gMySQLCon = dbConnect("mysql", "dbname=" .. database .. ";host=" .. hostname, username, password) if (not gMySQLCon) then cancelEvent(true, "[Error]: Database could not be connected!") return end outputServerLog("[Done]: Database connected!") end addEventHandler("onResourceStart", getResourceRootElement(), connectToDatabase, false) function getMySQLC() return gMySQLCon end Try This Code. @ironimust mtaserver.conf Did I enable module <module src="mta_mysql.dll" /> mysql running Link to comment
βurak Posted June 25, 2023 Share Posted June 25, 2023 (edited) 4 hours ago, ironimust said: not succesful Can you give us the error written in debugscript so that we can help you? or tell what is not working Edited June 25, 2023 by Burak5312 Link to comment
Prever77 Posted June 25, 2023 Share Posted June 25, 2023 (edited) local con = dbConnect("mysql","dbname=<database name>;host=127.0.0.1","login","password","share=1") addEventHandler("onResourceStart",resourceRoot,check) local function check() if con then outputServerLog("[database] connected with server") dbQuery(con,'SET NAMES utf8;') else outputServerLog("[database] can't find connection point") end end function set(x) if x then dbExec(con,x) end end function get(y) if y then local rawtab = dbQuery(con,y) local tab = dbPoll(rawtab,200) return tab end end Now XML file <meta> <script src="sside.lua" /> <export function="set" type="server" /> <export function="get" type="server" /> </meta> I did not tested it. But it should work. But remember that you need to call functions get() and set() with exports like: function thatDoSomething() if something then exports['x-x']:get('your php code') --use this if your resource have a special characters or-- exports.xx.get() --this for resource without specialcharacters --ofc xx is name of your resource end end Edited June 25, 2023 by Prever77 Little mistake. Link to comment
ironimust Posted May 4 Author Share Posted May 4 I answer a little late, this problem is modules that are not installed. ı fixed Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now