Load mysql
#1

Hi, in my database I have uid, nick, password.
For example:

uid|nick|password:
11|Oficer|123

And I want show "uid" when player is in game.

How I can do it?
Reply
#2

Do you have a player variable for uid?
Otherwise i suggest you create one and apply uid to that variable when the player connects.
Do you need help with that?
Reply
#3

No I don't have a variable for uid, I started with mysql.
If you have a moment, I would be grateful for help
Reply
#4

Okay. Which version of MySQL are you using?
Reply
#5

MySQL BlueG - R41
Reply
#6

pawn Код:
#define MYSQL_HOST "example host" // put your mysql host here
#define MYSQL_USER "example user" // put your mysql username here
#define MYSQL_DBNAME "example dbname" // put your mysql database name here
#define MYSQL_PASSWORD "example password" // put your mysql password here
#define MAX_PASSWORD_LEN 50 // put your max password length here.

new g_MySQLHandle;

enum e_PlayerData {
    e_PlayerUID,
    e_PlayerPassword[MAX_PASSWORD_LEN+1 char], // +1 for null terminator, char means packed string
    e_PlayerScore
}

new g_PlayerData[MAX_PLAYERS][e_PlayerData];

public OnGameModeInit() {
    g_MySQLHandle = mysql_connect(.host[] = MYSQL_HOST , .user[] = MYSQL_USER , .database[] = MYSQL_DBNAME, .password = MYSQL_PASSWORD);
}

public OnGameModeExit() {
    mysql_close(g_MySQLHandle);
}

public OnPlayerConnect(playerid) {
    new playername[MAX_PLAYER_NAME+1], querystr[100];
    GetPlayerName(playerid, playername, sizeof playername);
    format(querystr, sizeof querystr, "SELECT * FROM `put your tablename here` WHERE `nick`= '%q'", playername);
    mysql_tquery(g_MySQLHandle, querystr, "OnPlayerDataLoaded", "i", playerid);
    return 1;
}

forward OnPlayerDataLoaded(playerid);
public OnPlayerDataLoaded(playerid) {
    if( !IsPlayerConnected(playerid) ) {
        return 1; // the player disconnected before the data got loaded
    }

    if( cache_get_row_count(g_MySQLHandle) == 0 ) {
        return 1; // no rows were found / username was not found
    }

    cache_get_field_content_int(0, "uid", g_PlayerData[playerid][e_PlayerUID]); // load userid into playerdata userid

    // load password into temporary variable, then pack it into playerdata password
    new password_str[MAX_PASSWORD_LEN+1];
    cache_get_field_content(0, "password", password_str);
    strpack(g_PlayerData[playerid][e_PlayerPassword], password_str, MAX_PASSWORD_LEN+1);

    cache_get_field_content_int(0, "score", g_PlayerData[playerid][e_PlayerScore]); // load score into playerdata score (just an example, i know you did not include score)
    return 1;
}
I've made something for you. Use this as learning material and alter it to your needs. Good luck!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)