SA-MP Forums Archive
MySQL question - 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)
+--- Thread: MySQL question (/showthread.php?tid=606903)



MySQL question - NeXoR - 11.05.2016

Hey guys,
I'm changing my saving method from Dini to MySQL
I have finished the registration & login and now I have to modify all the offline commands
Offline Bans, Offline Admin removals, Offline leadership removals etc etc

My question is,
How can I do this:
PHP код:
    format(filesizeof(file), "users/%s.ini"playerb);
    if(!
dini_Exists(file)) return SendClientMessage(playeridCOLOR_GREY"Player name not found.");
    if(
PlayerInfo[playerid][pAdmin] < dini_Int(file"Admin")) return SendClientMessage(playeridCOLOR_GREY"Player has a higher admin level than you."); 
In a MySQL way WITHOUT using further callbacks


Re: MySQL question - Gammix - 11.05.2016

Using mysql_query i guess.
pawn Код:
"SELECT * FROM `table` WHERE `condition` = 'match'"
And then just check if there are any rows or not using cache_get_row_count (0 means not existing).

You can also checkout sql.inc which allows you MySQL with SQLite syntax and lot more, in case you are interested or unaware.


Re: MySQL question - ]Rafaellos[ - 11.05.2016

pawn Код:
new
    Cache:result,
    Query[128]
;

mysql_format(MySQL, Query, sizeof(Query), "SELECT `Admin` FROM `Users` WHERE `Username` = '%e'", playerb);
result = mysql_query(MySQL, Query);

if(cache_get_row_count(MySQL))
{
    new
        adminLevel
    ;

    adminLevel = cache_get_row_int(0, 0, MySQL);
       
    if(PlayerInfo[playerid][pAdmin] < adminLevel)
    {
        SendClientMessage(playerid, COLOR_GREY, "Player has a higher admin level than you.");
    }
    else
    {
        mysql_format(MySQL, Query, sizeof(Query), "UPDATE `Users` SET `Admin` = %d WHERE `Username` = '%e'", newLevel, playerb);
        mysql_tquery(MySQL, Query, "", "");
    }
}
cache_delete(result);
Untested but it will give you the idea.


Re: MySQL question - NeXoR - 11.05.2016

Quote:
Originally Posted by ]Rafaellos[
Посмотреть сообщение
pawn Код:
new
    Cache:result,
    Query[128]
;

mysql_format(MySQL, Query, sizeof(Query), "SELECT `Admin` FROM `Users` WHERE `Username` = '%e'", playerb);
result = mysql_query(MySQL, Query);

if(cache_get_row_count(MySQL))
{
    new
        adminLevel
    ;

    adminLevel = cache_get_row_int(0, 0, MySQL);
       
    if(PlayerInfo[playerid][pAdmin] < adminLevel)
    {
        SendClientMessage(playerid, COLOR_GREY, "Player has a higher admin level than you.");
    }
    else
    {
        mysql_format(MySQL, Query, sizeof(Query), "UPDATE `Users` SET `Admin` = %d WHERE `Username` = '%e'", newLevel, playerb);
        mysql_tquery(MySQL, Query, "", "");
    }
}
cache_delete(result);
Untested but it will give you the idea.
Well unfortunately I didn't know about that Cache: variable
I'll try it out

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Using mysql_query i guess.
pawn Код:
"SELECT * FROM `table` WHERE `condition` = 'match'"
And then just check if there are any rows or not using cache_get_row_count (0 means not existing).

You can also checkout sql.inc which allows you MySQL with SQLite syntax and lot more, in case you are interested or unaware.
Thanks, I'll take a look


Re: MySQL question - ]Rafaellos[ - 11.05.2016

Quote:
Originally Posted by NeXoR
Посмотреть сообщение
Well unfortunately I didn't know about that Cache: variable
I'll try it out
Well, if you want it WITHOUT further callbacks, you have to use Cache.


Re: MySQL question - Sjn - 11.05.2016

I'd still go with callbacks rather than using cached variables. It's way more easier (for me). And instead of creating way too many callbacks, simply create one callback using separate thread ids. For me, it's pretty faster.


Re: MySQL question - NeXoR - 11.05.2016

Quote:
Originally Posted by Sjn
Посмотреть сообщение
I'd still go with callbacks rather than using cached variables. It's way more easier (for me). And instead of creating way too many callbacks, simply create one callback using separate thread ids. For me, it's pretty faster.
I thought about creating a callback for all offline modification commands, But I don't know how to set an ID for each CMD, It would be great if you explain me


Re: MySQL question - Sjn - 11.05.2016

I have many result ids defined in the following ways for separate threads.
PHP код:
#define SQL_THREAD_NONE             0 // Thread used for executing normal queries (no result)
#define SQL_THREAD_OFFLINEBAN       1
#define SQL_THREAD_OFFLINELEVEL     2 
And for a callback I have something like
PHP код:
// You can add other parameters in the public callback for more values
forward OnQueryReceive(result_idint_valuestring[], handle);
public 
OnQueryReceive(result_idint_valuestring[], handle)
{
    new 
rowsfields;

    if (
result_id != SQL_THREAD_NONE// If the thread has no result, just execute the query without fetching the data
    
{
        
cache_get_data(rowsfields);
    }
    
    switch (
result_id)
    {
        case 
SQL_THREAD_OFFLINEBAN:
        {
            if (
rows)
            {
                
// Row exists, code here
            
}
            else
            {
                
// Row doesn't exist, return error or something
            
}
        }
        
// More threads bellow
    
}

    return 
1;

And for executing the query
PHP код:
mysql_tquery(handleQuery"OnQueryReceive""iis"SQL_THREAD_OFFLINEBANinteger_valstring_val); 
This is how I use them, all of my queries gets executed through one callback using separated thread ids.


Re: MySQL question - Slawiii - 11.05.2016

what's mean integer_val and string_val

i want to trasfer this from mysql r5 to mysql r 33+

PHP код:
    format(querysizeof(query), "SELECT * FROM `players` WHERE `name` = '%s'"PlayerName);
    
mysql_query(queryMYSQL_QUERY_CONNECTplayerid); 



Re: MySQL question - Slawiii - 13.05.2016

BUMP