MYSQL ORM Problem
#1

I can't realise why in the hell this doesen't work!
pawn Код:
#include < a_samp >
#include <a_mysql>
#include < zcmd >

#define DIALOG_LOGIN 1
#define DIALOG_REGISTER 2

#define MySQL_Host      "localhost"
#define MySQL_User      "root"
#define MySQL_Pass      ""
#define MySQL_DB        "dwr"

public OnFilterScriptInit( )
{
    mysql_debug( 1 );
    mysql_connect( MySQL_Host, MySQL_User, MySQL_DB, MySQL_Pass );
    return 1;
}

public OnFilterScriptExit( )
{
    mysql_close( );
    return 1;
}

enum E_PLAYER
{
    ORM:ORM_ID,
    ID,
    Name[MAX_PLAYER_NAME+1],
    Money,
    Level,
    Password[50]
};
new Player[MAX_PLAYERS][E_PLAYER];

public OnPlayerConnect(playerid)
{

    GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
    new ORM:ormid = Player[playerid][ORM_ID] = orm_create("players");

    orm_addvar_int(ormid, Player[playerid][ID], "ID");
    orm_addvar_string(ormid, Player[playerid][Name], MAX_PLAYER_NAME+1, "Name");
    orm_addvar_string(ormid, Player[playerid][Password],49, "Password");
    orm_addvar_int(ormid, Player[playerid][Money], "Money");
    orm_addvar_int(ormid, Player[playerid][Level], "Level");

    orm_setkey(ormid, "ID");
    orm_select(ormid, "OnPlayerDataLoad", "d", playerid);
    return 1;
}

forward OnPlayerDataLoad(playerid);
public OnPlayerDataLoad(playerid)
{
    switch(orm_errno(Player[playerid][ORM_ID]))
    {
        case ERROR_OK:
        {
            ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Please insert your password.", "Login", "Abort");
        }
        case ERROR_NO_DATA:
        {
            ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Register", "Please insert your password.", "Register", "Abort");
        }
    }
    orm_setkey(Player[playerid][ORM_ID], "ID");
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(Player[playerid][ID] >= 0)
    {
        orm_update(Player[playerid][ORM_ID]);
    }
    orm_destroy(Player[playerid][ORM_ID]);

    for(new E_PLAYER:e; e < E_PLAYER; ++e)
        Player[playerid][e] = 0;
    return 1;
}

public OnDialogResponse( playerid, dialogid, response, listitem, inputtext[ ] )
{
    if ( dialogid == DIALOG_REGISTER )
    {
        if ( response )
        {
            if ( strlen( inputtext ) == 0 ) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Please insert your password.", "Login", "Abort");
            orm_insert(Player[playerid][ORM_ID], "OnPlayerRegister", "d", playerid);
        }else
        {
            Kick( playerid );
        }
    }
    if( dialogid == DIALOG_LOGIN )
    {
        if( response )
        {
            if ( strlen( inputtext ) == 0 ) return ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Please insert your password.", "Login", "Abort");
            orm_load( Player[playerid][ORM_ID], "OnPlayerDataLogin", "d", playerid );
        }else
        {
            Kick( playerid );
        }
    }
    return 1;
}

forward public OnPlayerRegister(playerid);
public OnPlayerRegister(playerid)
{
    printf("Player %d signed up and their ID value is %d.", playerid, Player[playerid][ID]);
}

forward public OnPlayerDataLogin( playerid );
public OnPlayerDataLogin( playerid )
{
    printf("Player %d logined and ID value is %d.", playerid, Player[playerid][ID]);
    printf( "Money = %i, Level = %i", Player[ playerid ][ Money ], Player[ playerid ][ Level ] );
   
    return 1;
}

CMD:insert( playerid, params[ ] )
{
    Player[ playerid ][ Money ] = 9999;
    Player[ playerid ][ Level ] = 9999;
   
    return 1;
}

CMD:read( playerid, params[ ] )
{
    printf( "Money: %d, Level: %d", Player[ playerid ][ Money ], Player[ playerid ][ Level ] );
    return 1;
}

CMD:update( playerid, params[ ] )
{
    orm_save( Player[ playerid ][ ORM_ID ] );
    return 1;
}

public OnQueryError( errorid, error[ ], callback[ ], query[ ], connectionHandle )
{
    switch( errorid )
    {
        case 1146:
        {
            printf( "No table found. Error %s, callback %s, query %s", error, callback, query );
            mysql_reconnect( connectionHandle );
        }
        case ER_SYNTAX_ERROR:
        {
            printf( "Something is wrong in your syntax, query: %s", query );
        }
    }
    return 1;
}
Well, actualy it works, except one thing.
It doesen't matter what name I use, everytime it tells me that is registered.
(Don't except to much of me cause' this is the first time I use mysql)
Reply
#2

pawn Код:
orm_setkey(ormid, "ID");
orm_select(ormid, "OnPlayerDataLoad", "d", playerid);
You are fetching data from DB, you specify primary key as ID, but don't set any value for it.

https://sampforum.blast.hk/showthread.php?tid=461766 - as you can see, in this example in first select, the primary key is player name, which already is assigned to Player[playerid][Name]. So
pawn Код:
orm_setkey(ormid, "ID");
//to
orm_setkey(ormid, "Name");
And later change key to id if you want.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)