MySQL store data
#1

Hi. Can someone explain to me how can I get the result of a Query?

I'm using this plugin:

https://sampwiki.blast.hk/wiki/MySQL/R33

And I don't know how to use the Cache.

I'm trying to do this loop:

PHP код:
    for(new 0MAX_SLOTSi++)
    {
        
format(querysizeof(query), "SELECT 'ID' FROM `users` WHERE 'Username' = '%s'"Nome(playerid));
        
mysql_query(mysqlquerytrue);
    } 
And I presume that the query wil return ID... but how can I get that??
Reply
#2

If you want to use non-threaded queries, you could use this example (directly taken from my filterscript).
Don't copy paste this as you don't have the required other functions this function needs, it's merely shown how you can load the data from a non-threaded query:
pawn Код:
// This function is called to load the police-stations from MySQL during OnFilterScriptInit
PoliceStations_Load()
{
    // Setup local variables
    new Rows, PoliceID, Name[50], Float:x, Float:y, Float:z, CountSuccess, CountFailed, Query[128], Cache:result;

    // Send a query to load all police-stations from MySQL
    format(Query, sizeof(Query), "SELECT * FROM %s", table_policestations);
    result = mysql_query(SQL_db, Query, true);
    // Print some debug info to the server console
    printf("*** Loading police-stations from MySQL using \"%s\"", Query);

    // Get the amount of rows (police-stations)
    Rows = cache_get_row_count(SQL_db);

    // If there are any rows (police-stations) loaded, load data and create them
    if (Rows >= 1)
    {
        // Loop through all rows
        for (new Row; Row < Rows; Row++)
        {
            // Load the data
            PoliceID = cache_get_field_content_int(Row, "ID", SQL_db);
            cache_get_field_content(Row, "Name", Name, SQL_db, sizeof(Name));
            x = cache_get_field_content_float(Row, "X", SQL_db);
            y = cache_get_field_content_float(Row, "Y", SQL_db);
            z = cache_get_field_content_float(Row, "Z", SQL_db);

            // Check if the PoliceID is invalid (out of range)
            if ((PoliceID < 1) || (PoliceID >= MAX_POLICESTATIONS))
            {
                // Count the amount of failed police-stations (invalid PoliceID's)
                CountFailed++;
                printf("*** ERROR: Invalid PoliceID found in table %s: %i", table_policestations, PoliceID);
                // Continue with the next police-station from the MySQL query
                continue;
            }

            // Create the police-station at the given PoliceID (there should be no duplicate police-stations with the same ID as the table-structure doesn't allow it, ID = primary key)
            SetupPoliceStation(x, y, z, Name, PoliceID);
            // Count the succesfully created police-stations
            CountSuccess++;
        }
    }

    // Print the amount of police-stations loaded for debugging
    printf("*** Police-stations loaded: %i (successful: %i, failed: %i)", Rows, CountSuccess, CountFailed);
    // Clear the cache to prevent memory-leaks
    cache_delete(result, SQL_db);

    return 1;
}
In case you want to use threaded queries (using the cache), see this (also taken directly from my filterscript):
pawn Код:
// Select the names of all company-members (select only the 25 highest contributors of the company)
mysql_format(SQL_db, Query, sizeof(Query), "SELECT PlayerName, CompanyContribution FROM %s WHERE CompanyID = '%i' ORDER BY CompanyContribution DESC LIMIT 0, 25", table_playerdata, CompID);
mysql_tquery(SQL_db, Query, "Company_ShowMembers", "i", playerid);



// This callback is called when a player selects the "Members" option from /company
forward Company_ShowMembers(playerid);
public Company_ShowMembers(playerid)
{
    // Setup local variables
    new Name[24], Contribution, DialogText[2000];

    // Loop through all results
    for (new i; i < cache_get_row_count(SQL_db); i++)
    {
        // Get the name of the member and his contribution
        cache_get_field_content(i, "PlayerName", Name, SQL_db, 130);
        Contribution = cache_get_field_content_int(i, "CompanyContribution", SQL_db);

        format(DialogText, sizeof(DialogText), "%s{FFFFFF}Name: {FFFF00}%s{FFFFFF}, Contribution: {FFFF00}%i\n", DialogText, Name, Contribution);
        ShowPlayerDialog(playerid, DialogNoResponse, DIALOG_STYLE_LIST, "Members of the company:", DialogText, "Select", "Cancel");
    }

    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)