SA-MP Forums Archive
[Tutorial] A tutorial on MySQL connections! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] A tutorial on MySQL connections! (/showthread.php?tid=563759)



A tutorial on MySQL connections! - Abagail - 16.02.2015

MySQL: Managing mysql connections

This is a small, simple tutorial regarding MySQL connections. It basically teaches you some tips and tricks for BlueG's MySQL plugin(r33 and further).

Using multiple database connections(r35+):

By default, the MySQL plug-in will only allow one connection. However, using mysql_option we can toggle this to allow for multiple simultanoues connections.

We simply do:
pawn Code:
mysql_option(DUPLICATE_CONNECTIONS, true);
What this does is set a variable that tells the plugin to allow / deny the usage of multiple MySQL connections. Obviously, if we set the boolean value to true, it will allow and with false deny.

This allows the following code to work properly, and not just replace the current MySQL connection like it would if DUPLICATE_CONNECTIONS wasn't enabled.

pawn Code:
mysql_connections[0] = mysql_connect("127.0.0.1", "root", "database", "");
mysql_connections[1] = mysql_connect("127.0.0.1", "root", "second_database", "");
Now, accordingly if we were to disable the use of multiple database connections using two mysql_connect natives would just replace the prior connection keeping the same connection handle.

Closing a mysql connection:
This part is very simple. Basically, all one has to do to close a MySQL connection is use mysql_close. An example is shown below,
pawn Code:
mysql_close(mysql_connections[0]);
And to reconnect a connected handle we can simply do:
pawn Code:
mysql_reconnect(mysql_connections[0]);
Checking if a connection handle is valid or not:

To check if a connection handle is valid, we can simply use mysql_errno(connectionhandle). If it's not valid, -1 will be returned.

Example:
pawn Code:
mysql_connections[0] = mysql_connect("127.0.0.1", "root", "", "database");
if(mysql_errno(mysql_connections[0]) == -1)
{
     CallLocalFunction("On_MySQLConnectFail", "d", mysql_connections[0]);
}
If you believe anything was unclear, or would like documentation on more MySQL functions.

EDIT: Some useful functions

pawn Code:
stock IsValidConnectionHandle(connectionhandle=1)
{
       if(mysql_errno(connectionhandle)== -1) return 0;
       return 1;
}



Re: A tutorial on MySQL connections! - Ryan_Bowe - 16.02.2015

I've been doing this since the release of R35 but it's nice to see a tutorial on it.


Re: A tutorial on MySQL connections! - Abagail - 16.02.2015

Quote:
Originally Posted by Ryan_Bowe
View Post
I've been doing this since the release of R35 but it's nice to see a tutorial on it.
Thanks! I know there may be some documentation on this, but this as a whole should help people new to MySQL scripting / SA-MP mysql usage out a bit.


Re: A tutorial on MySQL connections! - Vince - 16.02.2015

Multiple connections usually aren't necessary, but when you do use them you must pay extreme attention to include the correct connectionHandle in every function at all times otherwise you'll likely get weird screwups.

I am also not entirely sure if you need to set the option at all if different databases are concerned. I do use this option, but I connect to the same database, albeit with a different user every time. Makes auditing easier. Each active script has its own user with its own specific rights as to what tables it can access.


Re: A tutorial on MySQL connections! - Abagail - 16.02.2015

Well, from my knowledge - you HAVE to enable it if you want to have any connection handle other than 1. I can't confirm this however.


Re: A tutorial on MySQL connections! - Ryan_Bowe - 16.02.2015

When I first attempted to create multiple connections without setting the option, they were all set to 1.


Re: A tutorial on MySQL connections! - Abagail - 16.02.2015

Yeah, that's what it should do. Are the connections just using a different database as Vince said? I'd be interested to see if that changes anything.


Re: A tutorial on MySQL connections! - Ryan_Bowe - 16.02.2015

I can test in a little bit. On my laptop at the moment.


Re: A tutorial on MySQL connections! - Luis- - 20.02.2015

There's a typo in your tutorial.

These are wrong.
pawn Code:
mysql_connections[0] = mysql_connect("127.0.0.1", "root", "", "database");
mysql_connections[1] = mysql_connect("127.0.0.1", "root", "", "second_database");
Should be this;
pawn Code:
mysql_connections[0] = mysql_connect("127.0.0.1", "root", "database", "");
mysql_connections[1] = mysql_connect("127.0.0.1", "root", "second_database", "");



Re: A tutorial on MySQL connections! - Abagail - 20.02.2015

Didn't notice, thank you! Should be fixed now.