Getting string from SQL
#1

Alright so the problem is simple, Im trying to format a string from the sql and store it inside AccInfo[playerid][NickName]

PHP код:
cache_get_field_content(0"NickName"string);
format(AccInfo[playerid][NickName], 128string); 
The problem is quite simple, it simply displays loads 0

PHP код:
[20:21:280
[20:21:2845 player ID
printf
("%s",cache_get_field_content(0"NickName"string));
printf("%i player ID",AccInfo[playerid][ID]); 
Reply
#2

That printf statement is wrong. cache_get_field_content does not return anything. The result is put into the string that is passed by reference.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
That printf statement is wrong. cache_get_field_content does not return anything. The result is put into the string that is passed by reference.
I tried printing AccInfo[playerid][NickName] but it prints our UserName not the nickname. And they are no where related. The NickName gets inserted entirely separately
Reply
#4

Okay so I finally managed to get it correctly, but the problem now is that it isnt being copied.
PHP код:
cache_get_field_content(51"NickName"string);
    
format(AccInfo[playerid][NickName], 64string);
    
    
strcpy(AccInfo[playerid][NickName],string,64); 
The issue is, its being displayed as Null
PHP код:
format(string,sizeof(string),"Nickname: %s",AccInfo[playerid][NickName]); 
Reply
#5

Try:

pawn Код:
cache_get_field_content(51, "NickName", AccInfo[playerid][NickName], connectionhandle, 64);
It will load the data and put it inside your array at the same time.
https://sampwiki.blast.hk/wiki/MySQL/R33..._field_content
Reply
#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
#7

Quote:
Originally Posted by AmigaBlizzard
Посмотреть сообщение
Try:

pawn Код:
cache_get_field_content(51, "NickName", AccInfo[playerid][NickName], connectionhandle, 64);
It will load the data and put it inside your array at the same time.
https://sampwiki.blast.hk/wiki/MySQL/R33..._field_content
Still doesnt help out, it still displays the string as NULL in game

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
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.
Ill try to do something like this but Im using Tquerries
Reply
#8

Bump.
Still getting result as Null when i insert it into a variable
NickName[64],
Reply
#9

How are u perfoming the queries and loading the results? I mean, you should have something like this:

PHP код:
mysql_function_query(handle,"SELECT nickname FROM users WHERE userid=10",true,"MyResponse","d",playerid); 
PHP код:
// MyResponse
cache_get_data(rows,fields);
if(
rows)
{
    
// the 0 means the first row found.
    // found amount is the variable rows
    // for more rows result you should use a loop from 0 to rows - 1
    
cache_get_field_content(0,"nickname",string,handle,64);
    
// string now has the value of field nickname from userid 10
}
else
{
    
// found nothing  bla bla

Reply
#10

Quote:
Originally Posted by arakuta
Посмотреть сообщение
How are u perfoming the queries and loading the results? I mean, you should have something like this:

PHP код:
mysql_function_query(handle,"SELECT nickname FROM users WHERE userid=10",true,"MyResponse","d",playerid); 
PHP код:
// MyResponse
cache_get_data(rows,fields);
if(
rows)
{
    
// the 0 means the first row found.
    // found amount is the variable rows
    // for more rows result you should use a loop from 0 to rows - 1
    
cache_get_field_content(0,"nickname",string,handle,64);
    
// string now has the value of field nickname from userid 10
}
else
{
    
// found nothing  bla bla

Its a quite long OnAccountLoad functions.

Loading part for the NickName
PHP код:
cache_get_field_content(51"NickName"AccInfo[playerid][NickName], mysql64); 
Now if I just format string inside the OnAccountLoad it will properly print the result, I mean it will print their NickName but once I store it inside AccInfo[playerid][NickName] it will be set as NULL
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)