Jump to content

Mysql Connection Error.


Recommended Posts

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
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
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 by Burak5312
Link to comment

@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 by klue
fix.
Link to comment
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
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
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 by ZeDni
Link to comment
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 :D

Link to comment
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 by Prever77
Little mistake.
Link to comment
  • 10 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...