Jump to content

[HELP] Database cannot connect !


Recommended Posts

hi guys.

 

this is my mysql connection script :

function connectToDatabase(res)
	MySQLConnection = mysqlconnect(hostname, username, password, database, port)
	
	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)

 

and errors is :

attempt to call global 'mysqlconnect' (a nil value)

 

plz debug my database connection script

 

Link to comment
Just now, WorthlessCynomys said:

.sql files are containing the databases and tables. You have to use MTA's internal database functions


dbConnect()
dbQuery()
dbPoll()
dbExec()

To handle MySQL. You can connect to your database with dbConnect. There's no such function as mysqlconnect.

how ?

i don't know work with internal database functions

 

you can help me?

Link to comment

Go to the WIKI page of dbConnect. There are example lines that can be copy pasted into your code, so like if you use MySQL, then you copy paste the MySQL line, fill it out with your data and it will connect.

 

This is how you do the connection part. Doing anything other than this is done by the other 3 functions. Selecting data from the database is done with dbQuery. You have to provide the MySQL connection to it, then write an SQL query to select the data you want. An SQL query looks somehow like this: "SELECT * FROM accounts WHERE id=?". This will select everything in accounts table where the value of id equals to what you gave to it as an argument.

It looks like this:

queryhandle = dbQuery(connection, "SELECT * FROM accounts WHERE id=?", id);

The value of id in the query will be the value of the variable id.

You can process the retrieved data with dbPoll:

result = dbPoll(queryhandle, 500);

This will give you a Lua table containing the lines that you got back. You can work with that table using a for cycle:

for i, row in ipairs(result) do -- i stands for Index and row stands for the rows that you got back from the database.
  outputChatBox(row["username"]); -- This will output the username value of every line.
end

If you don't want any return values at an update or insert, you can use dbExec:

dbExec(connection, "INSERT INTO accounts SET username=?", username);

These are the very basics. MySQL isn't hard, it's just a complete mess and this makes it hard to understand. Once you feel it, it's one of the easiest things in the world :D.

Good luck.

Link to comment
8 minutes ago, WorthlessCynomys said:

Go to the WIKI page of dbConnect. There are example lines that can be copy pasted into your code, so like if you use MySQL, then you copy paste the MySQL line, fill it out with your data and it will connect.

 

This is how you do the connection part. Doing anything other than this is done by the other 3 functions. Selecting data from the database is done with dbQuery. You have to provide the MySQL connection to it, then write an SQL query to select the data you want. An SQL query looks somehow like this: "SELECT * FROM accounts WHERE id=?". This will select everything in accounts table where the value of id equals to what you gave to it as an argument.

It looks like this:


queryhandle = dbQuery(connection, "SELECT * FROM accounts WHERE id=?", id);

The value of id in the query will be the value of the variable id.

You can process the retrieved data with dbPoll:


result = dbPoll(queryhandle, 500);

This will give you a Lua table containing the lines that you got back. You can work with that table using a for cycle:


for i, row in ipairs(result) do -- i stands for Index and row stands for the rows that you got back from the database.
  outputChatBox(row["username"]); -- This will output the username value of every line.
end

If you don't want any return values at an update or insert, you can use dbExec:


dbExec(connection, "INSERT INTO accounts SET username=?", username);

These are the very basics. MySQL isn't hard, it's just a complete mess and this makes it hard to understand. Once you feel it, it's one of the easiest things in the world :D.

Good luck.

i understand ;)

Good LuCk!

Link to comment

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...