SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 01/07/2020, 12:43 AM   #1
axmen_xxx
Little Clucker
 
Join Date: Oct 2011
Posts: 34
Reputation: 1
Question Doubtful use of MySQL

Hi, how are you? I recently started using MySQL R41-4. Before, I used a much older one, a question that now I am wondering how to use the queries towards the DB ("Select"). I saw that in some cases you can use cache and thus use rows to get data (mostly in commands). But I also saw that queries can be made through callbacks.
The question is, when they recommend using each of the two options.
axmen_xxx is offline   Reply With Quote
Old 01/07/2020, 08:34 AM   #2
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 215
Reputation: 43
Default Re: Doubtful use of MySQL

Quote:
Originally Posted by axmen_xxx View Post
Hi, how are you? I recently started using MySQL R41-4. Before, I used a much older one, a question that now I am wondering how to use the queries towards the DB ("Select"). I saw that in some cases you can use cache and thus use rows to get data (mostly in commands). But I also saw that queries can be made through callbacks.
The question is, when they recommend using each of the two options.
Through custom functions is always a nice and tidy approach, myself personally I don't know if I am doing it correct but it works flawlessly.

How I manage to load stuff, which is the correct way is yes cache, with rows.

heres a little snippet.

PHP Code:
OnGameModeInit...
LoadDynamicAtms(); 
PHP Code:
LoadDynamicAtms()
{
    
printf("==========================================================================");
    
printf("|+|+|+|+|+| Loading Dynamic Atms From the MySQL Database |+|+|+|+|+|");
    
printf("==========================================================================");
    new 
rows;
    
mysql_query(g_SQL"SELECT * FROM `atms` ORDER BY `id` ASC");
    if(
cache_get_row_count(rows))
    {
        for(new 
0rows && MAX_ATMSr++)
        {
            
cache_get_value_name_int(r"id"DynamicAtms[r][ID]);
            
cache_get_value_name_int(r"modelid"DynamicAtms[r][ModelID]);
            
cache_get_value_name_float(r"x"DynamicAtms[r][PosX]);
            
cache_get_value_name_float(r"y"DynamicAtms[r][PosY]);
            
cache_get_value_name_float(r"z"DynamicAtms[r][PosZ]);
            
cache_get_value_name_float(r"a"DynamicAtms[r][PosA]);
            
cache_get_value_name_int(r"interiorid"DynamicAtms[r][IntID]);
            
cache_get_value_name_int(r"worldid"DynamicAtms[r][VWID]);
            
cache_get_value_name_int(r"streamdistance"DynamicAtms[r][StreamDistance]);
            
SyncDynamicAtms(r);
        }
    }
    
printf("%i Dynamic Atm(s) loaded from the Database!"rows);
    return 
1;

You can see there I am running a non threaded query, however this is only in the case of "Loading" during initial startup, I want 1 query to be executed at a time, 1 table at a time, its a slightly slower process but it just seems better in my honest opinion, BUTTT! you don't use non-threaded queries within the code, for example if youre updating data during the servers up-time, you use a threaded query so it runs in the background, so workload isnt stressing server out.

Anyway, now that ive loaded all the Data from MySQL, stored the data into an Enumerator, I can just use the data at my free will.

PHP Code:
SyncDynamicAtms(atmid)
{
    
DestroyDynamicMapIcon(DynamicAtms[atmid][MapIconID]);
    
DestroyDynamicObject(DynamicAtms[atmid][ObjectID]);
    
DynamicAtms[atmid][MapIconID] = CreateDynamicMapIcon(DynamicAtms[atmid][PosX], DynamicAtms[atmid][PosY], DynamicAtms[atmid][PosZ], 52, -1, -1, -1, -170);
    
DynamicAtms[atmid][ObjectID] = CreateDynamicObject(DynamicAtms[atmid][ModelID], DynamicAtms[atmid][PosX], DynamicAtms[atmid][PosY], DynamicAtms[atmid][PosZ], 0.00.0DynamicAtms[atmid][PosA], 00, -1100.0);

So threaded queries dont change the way it works much.
Lets say I purchased a Condom, you want to update that data as soon as it changes, to prevent dataloss.

For me its another function i've created.
all I have to do is UpdateSQLPlayerCondoms(playerid); after the value changes.

PHP Code:
UpdateSQLPlayerCondoms(playerid)
{
    new 
query[445];
    if (
Player[playerid][IsLoggedIn] == false) return 0;
    if(
Player[playerid][PlayerSpawnCalled] != false)
    {
        
mysql_format(g_SQLquerysizeof query"UPDATE `players` SET `condoms` = %i WHERE `id` = %d LIMIT 1"Player[playerid][Condoms], Player[playerid][ID]);
        
mysql_tquery(g_SQLquery);
    }

How it looks in the Server Console after starting the Server:
__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is offline   Reply With Quote
Old 01/07/2020, 09:48 AM   #3
SharpenBlade
Big Clucker
 
Join Date: May 2020
Posts: 175
Reputation: 23
Default Re: Doubtful use of MySQL

If you want to load something from the database at your server starting, use mysql_query. Doing so, your server will be frozen until it loads all the stuff and no player could interfer with the loading from the db. Otherwise, my advice is to use threaded queries.
SharpenBlade is offline   Reply With Quote
Old 01/07/2020, 08:22 PM   #4
axmen_xxx
Little Clucker
 
Join Date: Oct 2011
Posts: 34
Reputation: 1
Default Re: Doubtful use of MySQL

Well thank you very much for answering and helping me, so am I doing well when querying the database in this way?

When a player enters (OnPlayerConnect), I ask if he is registered in this way:

PHP Code:
mysql_format (SQLquerysizeof (query), "SELECT` adm`, `password`,` skin` FROM `accounts` WHERE` Nick` = '% e' LIMIT 1 "nick [playerid]);
mysql_tquery (SQLquery"ChekCont""d"playerid); 
After that query (and if you find the account, the query) you will get the dialog to register or login.

PHP Code:
public ChekCont(playerid)
{
    if (
cache_num_rows ()> 0)
    {
        
cache_get_value_int (0"adm"PlayerInfo [playerid] [staff]);
        
cache_get_value (0"password"PlayerInfo [playerid] [password], 30);
        
cache_get_value_int (0"skin"PlayerInfo [playerid] [skinid]);
        
// Login dialog
    
} else {
         
// Registration dialog
    
}
    return 
1;

What I have doubts about is commands like this:

PHP Code:
dcmd_apwwps(playeridparams[])
{
    if(
sscanf(params"i"id)) return SendClientMessage(playeridCOLOR_WHITE"* Write: {ffff00}/apwwps [playerid]");
    if(
GT[playerid] <= gettime()+1)
    {
        if(
Spawned[playerid] == 0) return SendClientMessage(playerid,COLOR_WHITE"You need to spawn to use this command.");
        new 
query[254], rows;
        
mysql_format(SQLquerysizeof(query), "SELECT `id` FROM `band` WHERE `Leader_Band` = '%s'"nick[playerid]);
        new 
Cache:result mysql_query(SQLquery);
        
cache_get_row_count(rows);
        if(
rows != 0)
        {
            
//(Read below because I have a question in this part to consult something)
            //Dialogue to invite
        
} else {
            
SendClientMessage(playerid,COLOR_WHITE"You need to be your clan leader to use this command.");
            return 
1;
        }
        
cache_delete(result);
    } else {
        
SendClientMessage(playerid,COLOR_WHITE"You still can't use this command.");
    }
    return 
1;

I have a question, as I could consult the db again with cache, it would be to consult the number of members and verify that the group is not full. Could I use another Cache: result with cache_get_row_count to be able to query or not?
Would the way I'm using the cache and other things be ok?
axmen_xxx is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Plugin r39-2 Error: Can't connect to local MySQL server through socket Emre__ Scripting Help 10 30/07/2014 08:26 AM
[Tutorial] [VIDEO] MySQL ORM r38: How to pair GM with MySQL & creation of registration/connection system cArnasLT Tutorials 3 19/05/2014 02:08 PM
BlueG MySQL plugin - Failed (plugins/mysql.so: undefined symbol: _Z13stringvprintfPKcPc) linuxthefish Plugin Development 10 13/07/2013 10:56 AM
[Tool/Web/Other] MySQL DB Manager - Easily manage your MySQL databases with this remote application fiki574 Tools and Files 13 22/07/2012 09:41 AM
[DOUBTFUL]: GetPlayerVersion Jovanny Scripting Help 13 10/03/2012 12:55 AM


All times are GMT. The time now is 05:41 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.