SA-MP Forums Archive
MySQL system (BlueG's R5) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: MySQL system (BlueG's R5) (/showthread.php?tid=514443)



MySQL system (BlueG's R5) - MattTucker - 21.05.2014

I know that R5 is not that recent release but I just started off with MySQL a few days ago, after watching tutorials about the SQL thing itself, queries, tables, rows, databases...etc I decided to try to make a simple log/register gamemode to check the my understanding/like a self-test.

So, here's the code
pawn Код:
public OnPlayerConnect(playerid)
{
    if(mysql_ping()) print("MySQL connection is still alive!"); // self explanatory
    if(!mysql_ping()) print("MySQL connection is dead!"); //    ^

//  new query[126]; // just ignore this query for now
//  format(query,sizeof(query), "SELECT name FROM users WHERE name = '%s'", GetName(playerid));
    mysql_query("SELECT name FROM users WHERE name = 'his'");
    mysql_store_result();
    if(mysql_num_rows() == 1)
    {
        SendClientMessage(playerid,-1, "You are registered.");
    }
    else
    {
        SendClientMessage(playerid,-1, "Doesn't exist.");
    }
    return 1;
}
Okay so I have the mysql_connect under OnGameModeInit, mysql_close under OnGameModeExit and I have the users table and created a new row for "his" as a name, and when I run
pawn Код:
SELECT name FROM users WHERE name='his'
on the SQL query it runs perfectly fine and returns the column name having 'his'

The problem is, when I login into the game after hosting the GM, it says "Doesn't Exist", I thought it was something with the query that I formated, decided to try a normal one like name = 'his' after it worked on the sql query (phpmyadmin one) and yet still fails. So I kinda need help with mysql_store_results and mysql_num_rows on how to use/advices..etc


EDIT:I have the mysql_ping check on OnPlayerSpawn, OnPlayerRequestSpawn,OnplayerConnect OnPlayerDisconnect and it says connection it is alive.

EDIT#2:
pawn Код:
stock GetName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,sizeof(name));
    return name;
}
Thanks in regards,
Matt Tucker.


Re: MySQL system (BlueG's R5) - MacT - 21.05.2014

You need use player stock inside mysql:
pawn Код:
format(string, sizeof(string), "SELECT * FROM `users` WHERE `username` = '%s'", MName(playerid));
//stock
stock MName(playerid)
    {
    new Name[24];
    GetPlayerName(playerid, Name, sizeof(Name));
    return Name;
    }



Re: MySQL system (BlueG's R5) - MattTucker - 22.05.2014

I will test it soon but why do I select all when I only need the name? Wouldn't it be unwise to select all if the variables were too much? (no offence)


Re: MySQL system (BlueG's R5) - MacT - 22.05.2014

No Problem, I am cold man, okay I didint noticed you edit2, okay that's fine and you don't need use my stock
I fixed you code and you need use string:
pawn Код:
public OnPlayerConnect(playerid)
            new query[128], pname[MAX_PLAYER_NAME];
            GetPlayerName(playerid, pname, sizeof(pname));
            format(query, sizeof(query), "SELECT `name` FROM `users` WHERE `username`='%e'", pname);
            mysql_query(query);
            mysql_store_result();
            if (mysql_fetch_field("name", data))
            {
            }
            else
            {
            SendClientMessage(playerid, -1, "You dont have account!");
            }
            mysql_free_result();



Re: MySQL system (BlueG's R5) - MattTucker - 22.05.2014

@MacT, it still says Doesn't exist.
Anyway thanks for helping I'll keep on trying to fix it myself.


Re: MySQL system (BlueG's R5) - MacT - 22.05.2014

Man, dont give up, your problem is my problem and we can try fix this together. I try fix you code. Have you setting up database file and uploaded to phpmyadmin ?


Re: MySQL system (BlueG's R5) - MattTucker - 22.05.2014

Yeah everything is set up having a useraccount with name and such, I'll try another way around though.

Nice spirit you got over there lol.


Re: MySQL system (BlueG's R5) - rappy93 - 22.05.2014

Not sure if this will help you that much but you can get an idea of how it should work.
pawn Код:
stock InitPlayerConnection( playerid )
{
    new Query[ 128 ], Name[ MAX_PLAYER_NAME ], EscapedName[ MAX_PLAYER_NAME ];
    GetPlayerName( playerid, Name, sizeof( Name ) );
    mysql_real_escape_string( Name, EscapedName );
    #pragma unused Name
    format( Query, sizeof( Query ), "SELECT `UserID` FROM `Accounts` WHERE `Username` = '%s'", EscapedName );
    mysql_query( Query );
    printf(" SQL: %s",Query);
    mysql_store_result();
    if( mysql_num_rows() >= 1 )
    {
        PlayerInfo[playerid][pDatabaseID] = mysql_fetch_int();
        // Start shit
        new string[128];
        format( string, sizeof( string ), "{FFFFFF}Welcome back {00C0FF}%s{FFFFFF}!\nYou have 60 seconds to login\nEnter Your Password", GetPlayerNameEx(playerid) );
        ShowPlayerDialog( playerid, 1, DIALOG_STYLE_INPUT,"{6EF83C}RP:RP{FFFFFF}",string, "Login", "Exit" );
        SetTimerEx("LogTimer", 60000, 0, "d", playerid);
        TogglePlayerSpectating(playerid, 1);
        gPlayerLogged[playerid] = 0;
        mysql_free_result();
    }
    else
    {
        mysql_free_result();
        gPlayerLogged[playerid] = 0;
        ShowPlayerDialog( playerid, 2, DIALOG_STYLE_INPUT, "{6EF83C}Registration Process{FFFFFF}", "Hello there!\n\nPlease enter a password to register.", "Register", "Cancel" );
    }
    return 1;
}



Re: MySQL system (BlueG's R5) - MacT - 22.05.2014

I just re-edit my code:
pawn Код:
public OnPlayerConnect(playerid)
{
            new query[128], pname[MAX_PLAYER_NAME];
            GetPlayerName(playerid, pname, sizeof(pname));
            format(query, sizeof(query), "SELECT `name` FROM `users` WHERE `username`='%e'", pname);
            mysql_query(query);
            mysql_store_result();
            if (mysql_fetch_field("name", data))
            {
            }
            else
            {
            SendClientMessage(playerid, -1, "You dont have account!");
            }
            mysql_free_result();
            return 1;
}
Maybe you need use real_escape for avoiding sql incjection.


Re: MySQL system (BlueG's R5) - MattTucker - 22.05.2014

Thank you guys for your help but still the same problem, I will re-check the tables database and everything again.

Rep'd you both.