15.10.2011, 03:02
Connecting to a MySQL server
by RealCop228
Please note that any links in the body of this tutorial have been colored black in order to keep with the color scheme.by RealCop228
Introduction
I am going to be writing a series of tutorials regarding integration of MySQL into your PAWN scripts. These tutorials will also be available for download (in PDF format) once my website is set up, respectively. Any further questions, comments, or concerns can be directed to my message box.
Materials
- BlueG's MySQL plugin
- a MySQL server to connect to
- experience in phpMyAdmin
You need to begin by downloading BlueG's MySQL plugin and placing the files in their respective places. Please note that if you will be running your server on Windows, you need plugin files ending in .dll. If you plan to use Linux for your server, you will need files ending in .so. If you plan to enhance your knowledge of MySQL beyond this series, I would recommend that you check out this website (click here).
Step Two
This step is optional, but recommended just for ease-of-use.
Go ahead and open up your current PAWN script (or begin a new one). You'll need to add the following lines towards the top of your script. I personally add them just before the main() function.
pawn Код:
#define SQL_HOST ""
#define SQL_USER ""
#define SQL_DATA ""
#define SQL_PASS ""
pawn Код:
#define SQL_HOST "localhost"
#define SQL_USER "root"
#define SQL_PASS ""
In Example:
pawn Код:
mysql_connect("host", "user", "database", "password");
Section A:
You need to add the following line when your GM/FS initiates:
pawn Код:
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
pawn Код:
public OnFilterScriptInit()
{
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
return 1;
}
public OnGameModeInit()
{
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
return 1;
}
This step is optional, but recommended because it will help you debug your system in the future.
Add the function mysql_debug() above the function you added in Step Three, Section A.
In Example:
pawn Код:
public OnFilterScriptInit()
{
mysql_debug(1);
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
return 1;
}
public OnGameModeInit()
{
mysql_debug(1);
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
return 1;
}
pawn Код:
mysql_debug(1);
Section C:
This step, along with Step Three, Section A is mandatory. Without this, your SA-MP server will never officially disconnect from your MySQL server and this could eventually cause your MySQL server not to function properly.
Add the function "mysql_close()" when your GM/FS exits.
In Example:
pawn Код:
public OnFilterScriptExit()
{
mysql_close();
return 1;
}
public OnGameModeExit()
{
mysql_close();
return 1;
}
Step Four
You are set to establish a connection to your MySQL server, congratulations if you have exceeded this far into the tutorial. However, don't close out this page just yet because you still don't have a database to use. This is where having phpMyAdmin knowledge will come in handy.
Your interface may be a little different than in the picture above, however most phpMyAdmin interfaces are quite similar if not 90% the same. In the blank box, input a database name and hit the "Create" button. Your database is ready to go.
If you recall correctly, in step two we didn't input a value for the "SQL_DATA" define, we'll do that now. The name of your database goes between the apostrophes, just like the other defines.
In Example:
pawn Код:
#define SQL_DATA "database1"
Congratulations, you have just set up your script to communicate with a MySQL server. Please look for more tutorials in this series. I'll be posting about 2 tutorials a week so stay tuned. Remember that these tutorials will be downloadable in PDF format once my website is set up!
Our finished product:
pawn Код:
#define FILTERSCRIPT
#include <a_samp>
#define SQL_HOST "localhost"
#define SQL_USER "root"
#define SQL_DATA "database1"
#define SQL_PASS ""
#if defined FILTERSCRIPT
public OnFilterScriptInit()
{
mysql_debug(1);
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
return 1;
}
public OnFilterScriptExit()
{
mysql_close();
return 1;
}
#endif
public OnGameModeInit()
{
mysql_debug(1);
mysql_connect(SQL_HOST, SQL_USER, SQL_DATA, SQL_PASS);
return 1;
}
public OnGameModeExit()
{
mysql_close();
return 1;
}
- You can find a complete list of BlueG's MySQL functions here.
- You can find great game/web hosting by visiting Volt Host.
- You can message me for any tutorial requests.
- Nick "RealCop228" Piccoli - Creating this tutorial and having the idea of a whole series.
- BlueG for the MySQL plugin
- Calg00ne for assisting me a long time ago in MySQL
- playbox12 for pretty much teaching me a ton of MySQL-related material