Getting string from SQL
#6

Here's an example:
pawn Код:
// ** INCLUDES

#include <a_samp>
#include <a_mysql>

// ** DEFINES

// *** DATABASE

#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "mysql_get_string"

// *** VEHICLES DATABASE

// **** TABLES

#define TABLE_STRINGS "strings"

// **** FIELDS

#define STRINGS_TEXT "text"

// ** VARIABLES

// *** GLOBAL VARIABLES

// **** DATABASE

static strings_database;

// ** MAIN

main()
{
    print("Loaded \"mysql_get_string.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
    LoadDatabase();
    FindString("test");
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** FUNCTIONS

stock LoadDatabase()
{
    mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG);
    strings_database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);

    if(mysql_errno(strings_database) != 0)
    {
        printf("[MySQL] Couldn't connect to %s.", MYSQL_DATABASE);
    }
    else
    {
        printf("[MySQL] Connected to %s.", MYSQL_DATABASE);

        new query[300], string[144];
        format(string, sizeof(string), "CREATE TABLE IF NOT EXISTS %s(", TABLE_STRINGS);
        strcat(query, string);

        format(string, sizeof(string), "%s VARCHAR(128))", STRINGS_TEXT);
        strcat(query, string);

        mysql_query(strings_database, query);
    }  
    return 1;
}

stock FindString(string[])
{
    new query[500];
    format(query, sizeof(query), "SELECT `%s` FROM `%s`", STRINGS_TEXT, TABLE_STRINGS);
    mysql_function_query(strings_database, query, true, "OnStringsQuery", "s", string);
    return 1;
}

forward OnStringsQuery(string[]);
public OnStringsQuery(string[])
{
    new result[128], bool:found = false;
    for(new i = 0, j = cache_get_row_count(strings_database); i < j; i ++)
    {
        cache_get_field_content(i, STRINGS_TEXT, result, strings_database, sizeof(result));

        if(!strcmp(result, string, false))
        {
            found = true;
            break;
        }
    }

    if(found)
    {
        printf("The string \"%s\" was found in the database!", string);
    }
    else
    {
        printf("The string \"%s\" wasn't found in the database.", string);
    }
    return 1;
}
But of course on your script, you would want to know the row ID to handle ("i" on my example) for better performance.
Reply


Messages In This Thread
Getting string from SQL - by TwinkiDaBoss - 09.12.2015, 18:24
Re: Getting string from SQL - by Vince - 09.12.2015, 18:47
Re: Getting string from SQL - by TwinkiDaBoss - 09.12.2015, 18:52
Re: Getting string from SQL - by TwinkiDaBoss - 09.12.2015, 20:37
Re: Getting string from SQL - by AmigaBlizzard - 09.12.2015, 20:57
Re: Getting string from SQL - by SickAttack - 09.12.2015, 21:07
Re: Getting string from SQL - by TwinkiDaBoss - 10.12.2015, 16:16
Re: Getting string from SQL - by TwinkiDaBoss - 10.12.2015, 23:17
Re: Getting string from SQL - by arakuta - 10.12.2015, 23:32
Re: Getting string from SQL - by TwinkiDaBoss - 10.12.2015, 23:36

Forum Jump:


Users browsing this thread: 3 Guest(s)