Help with MySQL queries
#1

Hey guys, well I've been scripting for a year or two now but I stopped for a while and have decided to go back to it now.. Problem is with these new 'threaded queries' as 'unthreaded queries' are not supported any longer.

I'm gradually understanding how threaded queries work but there is still is a few dark spots that i'm trying to understand, so possibly someone here could answer this for me.

When a player registers on my server it checks to see if they are banned... now this all worked previously but with these new threaded queries it's a little more complex it would seem.. Before i'd just simply do a query to the server as they connect to check if they were banned. Like such below:

pawn Code:
public OnPlayerConnect(playerid)
{
    TogglePlayerClock(playerid, 1);
    new query[200], Message[256];

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    format(query, sizeof(query), "SELECT * FROM `Banned` WHERE IP = '%s'", ip);
    mysql_query(query);
    mysql_store_result();


    if(mysql_num_rows() >= 1)
    {
        SCM(playerid, COLOR_RED, "You are banned from this server.");
        Kick(playerid);
    }
    mysql_free_result();

How ever, now it needs something in the scope such as a public call and forward... or is that necessary? Trying to understand the way the new threaded queries work is a bit hard for me to grasp. For example... This part

pawn Code:
mysql_function_query(dbHandle, query, true (This is the equivalent to mysql_store_result, right?) , "What Would Go Here?", "i - Would I need a player ID here to check if they are banned?", playerid);

In the that code above I have marked a few parts that I am just... finding hard to understand, someone with knowledge on threaded queries could really help! Thanks

~Liam
Reply
#2

I know that when you use SELECT you must use a forward and public to be able to work with selected content like :

This:
pawn Code:
public OnPlayerConnect(playerid)
{
    TogglePlayerClock(playerid, 1);
    new query[200], Message[256];

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    format(query, sizeof(query), "SELECT * FROM `Banned` WHERE IP = '%s'", ip);
    mysql_query(query);
    mysql_store_result();


    if(mysql_num_rows() >= 1)
    {
        SCM(playerid, COLOR_RED, "You are banned from this server.");
        Kick(playerid);
    }
    mysql_free_result();
will be:
pawn Code:
public OnPlayerConnect(playerid)
{
    TogglePlayerClock(playerid, 1);
    new query[200], Message[256];

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    format(query, sizeof(query), "SELECT * FROM `Banned` WHERE IP = '%s'", ip);
    mysql_function_query(dbHandel,query,true,"BanResult","i",playerid);
pawn Code:
forward BanResult(playerid);
public BanResult(playerid)
{
 new rows, fields;
 cache_get_data(rows, fields, dbHandle);
  if(rows >= 1)
    {
        SCM(playerid, COLOR_RED, "You are banned from this server.");
        Kick(playerid);
    }
return 1;
}

And BTW you should use a timer to kick player , if you don't he will not recieve the message since 0.3x
Reply
#3

Quote:
Originally Posted by shady001
View Post
I know that when you use SELECT you must use a forward and public to be able to work with selected content like :

This:
pawn Code:
public OnPlayerConnect(playerid)
{
    TogglePlayerClock(playerid, 1);
    new query[200], Message[256];

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    format(query, sizeof(query), "SELECT * FROM `Banned` WHERE IP = '%s'", ip);
    mysql_query(query);
    mysql_store_result();


    if(mysql_num_rows() >= 1)
    {
        SCM(playerid, COLOR_RED, "You are banned from this server.");
        Kick(playerid);
    }
    mysql_free_result();
will be:
pawn Code:
public OnPlayerConnect(playerid)
{
    TogglePlayerClock(playerid, 1);
    new query[200], Message[256];

    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));

    format(query, sizeof(query), "SELECT * FROM `Banned` WHERE IP = '%s'", ip);
    mysql_function_query(dbHandel,query,true,"BanResult","i",playerid);
pawn Code:
forward BanResult(playerid);
public BanResult(playerid)
{
 new rows, fields;
 cache_get_data(rows, fields, dbHandle);
  if(rows >= 1)
    {
        SCM(playerid, COLOR_RED, "You are banned from this server.");
        Kick(playerid);
    }
return 1;
}

And BTW you should use a timer to kick player , if you don't he will not recieve the message since 0.3x
That makes more sense to me, you see... Thanks for that! And with the message and 0.3x.. I stopped scripting at 0.3d so a lot needs updated, how come they will not receive the message?
Reply
#4

pawn Code:
public OnPlayerConnect( playerid )
{
    TogglePlayerClock( playerid, 1 );
    new
        query[ 200 ],
        Message[ 256 ],
        ip[ 16 ]
    ;
    GetPlayerIp( playerid, ip, sizeof( ip ) );

    format( uery, sizeof( query ), "SELECT * FROM `Banned` WHERE IP = '%s'", ip );
    mysql_function_query( Handle, query, true, "OnBanCheck", "d", playerid );
    // Handle = mysql_connect( .. );
}

forward OnBanCheck( playerid );
public OnBanCheck( playerid )
{
    new
        rows,
        fields
    ;
    cache_get_data( rows, fields, Handle );

    if( rows )
    {
        SCM( playerid, COLOR_RED, "You are banned from this server." );
        SetTimerEx( "KickBannedPlayer", 500, false, "i", playerid );
    }
    else
    {
        // goto for register;
    }
    return 1;
}

forward KickBannedPlayer( playerid );
public KickBannedPlayer( playerid )
{
    return Kick( playerid );
}
pawn Code:
mysql_function_query(connectionHandle, query[], bool:cache, callback[], format[], {Float,_}:...);
EDIT: Too late.
Reply
#5

pawn Code:
SetTimerEx("KickPlayer",500,false,"i",playerid);// Kicks player in 500ms

}
forward KickPlayer(playerid);
public KickPlayer(playerid)
{
    Kick(playerid);
    return 1;
}
Any code placed before Kick() will not be executed because Kick() have priority above all other comands...

This is why you have to use timer if you want the client to recieve any messages

Edit : and more about mysql(from where i've learned) : https://sampforum.blast.hk/showthread.php?tid=337810
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)