[Tutorial] Connecting to a MySQL server
#1

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.

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
Step One

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 ""
Those defines will be a handy place for you to change your MySQL server information at ease without needing to do so in the actual "mysql_connect()" function. If you are currently running a WAMP server, your default settings should be the following:

pawn Код:
#define SQL_HOST "localhost"
#define SQL_USER "root"
#define SQL_PASS ""
As this is an optional step, you don't need to use defines. However, if you choose not to use them you can input your MySQL server information into the mysql_connect() function. You'll learn more about this function in Step Three, Section A.

In Example:

pawn Код:
mysql_connect("host", "user", "database", "password");
Step Three

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);
In Example:

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;
}
Section B:

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;
}
Notice the entire function:

pawn Код:
mysql_debug(1);
Specifically, the number one (1) in the function. Without that number, this function will most likely return an error when you compile. Adding number one signifies that you wish to enable MySQL debugging, adding a zero (0) would signify that you wish to disable MySQL debugging.

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;
}
Once this function is called, it will close the connection between your SA-MP server and your MySQL server. This will prevent you from reaching your max amount of connections and eventually stopping your SA-MP server from connecting to your MySQL server. You don't want to call this function anywhere else besides when your GM/FS closes, otherwise your server will crash.

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"
Conclusion

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;
}
Resources
  • 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.
Credits/Special Thanks
  • 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
Reply
#2

Reserved
Reply
#3

Thanks, awesome tutorial.
Do you know how to create a top5 with mysql ?
sorry for this question..
Reply
#4

Quote:
Originally Posted by Ricop522
Посмотреть сообщение
Thanks, awesome tutorial.
Do you know how to create a top5 with mysql ?
sorry for this question..
Do you mean something like a list of the players with the top 5 amounts of cash?
Reply
#5

Quote:
Originally Posted by Ricop522
Посмотреть сообщение
Thanks, awesome tutorial.
Do you know how to create a top5 with mysql ?
sorry for this question..
that is easy you need to do in php
and
Nice tutorial
Reply
#6

Quote:
Originally Posted by Venice
Посмотреть сообщение
that is easy you need to do in php
and
Nice tutorial
You don't need to do it in PHP, you can easily do this in PAWN too. Read this post.

A few suggestions for this tutorial would be
1. Tell them where they can find the MySQL log.
2. You should tell them what issues can arise E.g. not being able to connect to the database.
Reply
#7

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
You don't need to do it in PHP, you can easily do this in PAWN too. Read this post.

A few suggestions for this tutorial would be
1. Tell them where they can find the MySQL log.
2. You should tell them what issues can arise E.g. not being able to connect to the database.
I mean in website
Reply
#8

Quote:
Originally Posted by Venice
Посмотреть сообщение
that is easy you need to do in php
and
Nice tutorial
..

By Calg00ne

pawn Код:
mysql_query("SELECT `Name`, `Score` FROM `players` ORDER BY `Score` DESC LIMIT 10");
mysql_store_result();
new szPrepTD[460], iIterator = 1, iScore, szName[MAX_PLAYER_NAME], szUnload[64];
while(mysql_fetch_row_format(szUnload)) {
// This code will repeat ten times.
sscanf(szUnload, "p<|>s[24]d", szName, iScore);
if(strlen(szPrepTD) == 0) format(szPrepTD, sizeof(szPrepTD), "#%d - %s (%d score)\n", iIterator, szName, iScore);
format(szPrepTD, sizeof(szPrepTD), "%s#%d - %s (%d score)\n", szPrepTD, iIterator, szName, iScore);
iIterator++;
}
mysql_free_result();*/
Reply
#9

Good job, about time somebody made a high-quality tutorial
Reply
#10

Quote:
Originally Posted by Kar
Посмотреть сообщение
..

By Calg00ne

pawn Код:
mysql_query("SELECT `Name`, `Score` FROM `players` ORDER BY `Score` DESC LIMIT 10");
mysql_store_result();
new szPrepTD[460], iIterator = 1, iScore, szName[MAX_PLAYER_NAME], szUnload[64];
while(mysql_fetch_row_format(szUnload)) {
// This code will repeat ten times.
sscanf(szUnload, "p<|>s[24]d", szName, iScore);
if(strlen(szPrepTD) == 0) format(szPrepTD, sizeof(szPrepTD), "#%d - %s (%d score)\n", iIterator, szName, iScore);
format(szPrepTD, sizeof(szPrepTD), "%s#%d - %s (%d score)\n", szPrepTD, iIterator, szName, iScore);
iIterator++;
}
mysql_free_result();*/
I don't really understand your point?

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
Good job, about time somebody made a high-quality tutorial
Thanks!
Reply
#11

Good job, hope there will be less questions on how to get started now.

(and thanks for including me in the credits).
Reply
#12

Good tutorial RealCop228, nicely explained
Reply
#13

Coming up next will be a tutorial on setting up tables in phpMyAdmin for use with your SA-MP server.
Reply
#14

Quote:
Originally Posted by RealCop228
View Post
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.
Not necessarily. When the plugin's deconstructor is called, it will automatically disconnect from the MySQL database without having mysql_close.
Reply
#15

Quote:
Originally Posted by [NoV]LaZ
View Post
Not necessarily. When the plugin's deconstructor is called, it will automatically disconnect from the MySQL database without having mysql_close.
I don't believe that's true. I was always told that the function mysql_close() was mandatory to include.
Reply
#16

Very nice RealCop228.
Reply
#17

nice tutorial.
Reply
#18

Thanks you for this Tutorial useful
Reply
#19

I'm working on the next phase of this tutorial, where you can setup tables for use with your SA-MP server.
Reply
#20

not work for me -_-
PHP Code:
[10:16:37] [debugRun time error 19"File or function is not found"
[10:16:37] [debugmysql_query
[10:16:37] [debugmysql_store_result
[10:16:37] [debugmysql_free_result
[10:16:37] [debugmysql_close
[10:16:37] [debugmysql_real_escape_string
[10:16:37] [debugmysql_insert_id
[10:16:37] [debugmysql_retrieve_row
[10:16:37] [debugmysql_fetch_field_row
[10:16:37] [debugmysql_num_rows
[10:16:37] [debugmysql_connect
[10:16:37] [debugmysql_ping
[10:16:37] [debugmysql_debug
[10:16:37] [debugRun time error 19"File or function is not found"
[10:16:37] [debugmysql_query
[10:16:37] [debugmysql_store_result
[10:16:37] [debugmysql_free_result
[10:16:37] [debugmysql_close
[10:16:37] [debugmysql_real_escape_string
[10:16:37] [debugmysql_insert_id
[10:16:37] [debugmysql_retrieve_row
[10:16:37] [debugmysql_fetch_field_row
[10:16:37] [debugmysql_num_rows
[10:16:37] [debugmysql_connect
[10:16:37] [debugmysql_ping
[10:16:37] [debugmysql_debug
[10:16:37Script[gamemodes/xSF.amx]: Run time error 19"File or function is not found"
[10:16:37Number of vehicle models
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)