mysql - How to check if username exists
#1

Hi, i've started to use mysql, but i have a problem.
How can i use mysql to check every registered username that are in my database?

Like i joined my server with the name "Naxix" then i want the database to check if "Naxix" is in the database. I tried with this code:

pawn Код:
dcmd_register(playerid,params[])
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,sizeof(name));
    new Query[200], Pname[24], escpname[24];
    GetPlayerName(playerid, Pname, 24);
    mysql_real_escape_string(Pname, escpname);
    format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'", escpname);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows() != 0) return SendClientMessage(playerid,COLOUR_RED,"This account name is already in use!"); // I tried with this, but it wont work, i was logged in but i can just keep register a new account.
    {
        new PIP[50],pass[128];
        GetPlayerName(playerid, Pname, 24);
        new escpass[100];
        if(sscanf(params,"s",pass)) return SendClientMessage(playerid,COLOUR_RED,"Usage: /register [Password]");
        mysql_real_escape_string(pass, escpass);
        mysql_real_escape_string(Pname, escpname);
        GetPlayerIp(playerid, PIP, 50);
        format(Query, sizeof(Query), "UPDATE `playerinfo` SET `score` = '%d',`money` = '%d' WHERE `user` = '%s'", GetPlayerScore(playerid), GetPlayerMoney(playerid), escpname);
    mysql_query(Query);
        GameTextForPlayer(playerid, "~g~Registered", 2000, 3);
        SendClientMessage(playerid, 0x0000D9AA, "Registered and Logged into your account!");
        SetPVarInt(playerid, "Logged", 1);
    }
    return 1;
}
I can just keep on register a new account.
Reply
#2

pawn Код:
if ( mysql_num_rows ( ) != 0 ) return BLAHBLAH;
else
{
That?
Reply
#3

remove the {} or add else like Retardedwolf said
Reply
#4

It does still let me keep on register new accounts with the same name.
Reply
#5

check your database if the account exist there after you register... and check that the fields at the sql command are the same as in your database

Q: its a hosted server or a private one?
(if private make sure WAMP is on)
Reply
#6

Lol, thanks.. I feel dumb i forgot to turn on wamp
But thank you so much!
Reply
#7

The problem is in your syntax, not in the MySQL check.

pawn Код:
dcmd_register(playerid,params[])
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,sizeof(name));
    new Query[200], Pname[24], escpname[24];
    GetPlayerName(playerid, Pname, 24);
    mysql_real_escape_string(Pname, escpname);
    format(Query, sizeof(Query), "SELECT user FROM `playerinfo` WHERE `user` = '%s'", escpname);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows() != 0) return SendClientMessage(playerid,COLOUR_RED,"This account name is already in use!"); // I tried with this, but it wont work, i was logged in but i can just keep register a new account.
    else
    {
        new PIP[50],pass[128];
        GetPlayerName(playerid, Pname, 24);
        new escpass[100];
        if(sscanf(params,"s",pass)) return SendClientMessage(playerid,COLOUR_RED,"Usage: /register [Password]");
        mysql_real_escape_string(pass, escpass);
        mysql_real_escape_string(Pname, escpname);
        GetPlayerIp(playerid, PIP, 50);
        format(Query, sizeof(Query), "UPDATE `playerinfo` SET `score` = '%d',`money` = '%d' WHERE `user` = '%s'", GetPlayerScore(playerid), GetPlayerMoney(playerid), escpname);
    mysql_query(Query);
        GameTextForPlayer(playerid, "~g~Registered", 2000, 3);
        SendClientMessage(playerid, 0x0000D9AA, "Registered and Logged into your account!");
        SetPVarInt(playerid, "Logged", 1);
    }
    return 1;
}
Also what's the need for select *? You don't need to use any of the information in the columns, so there's no reason to select it all, if you have a lot of columns this may induce performance issues on the server.

Edit: Too late , anyway the select * stuff still applies
Reply
#8

any time ... happened to me too couple of time XD
Reply
#9

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
The problem is in your syntax, not in the MySQL check.

pawn Код:
dcmd_register(playerid,params[])
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,sizeof(name));
    new Query[200], Pname[24], escpname[24];
    GetPlayerName(playerid, Pname, 24);
    mysql_real_escape_string(Pname, escpname);
    format(Query, sizeof(Query), "SELECT user FROM `playerinfo` WHERE `user` = '%s'", escpname);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows() != 0) return SendClientMessage(playerid,COLOUR_RED,"This account name is already in use!"); // I tried with this, but it wont work, i was logged in but i can just keep register a new account.
    else
    {
        new PIP[50],pass[128];
        GetPlayerName(playerid, Pname, 24);
        new escpass[100];
        if(sscanf(params,"s",pass)) return SendClientMessage(playerid,COLOUR_RED,"Usage: /register [Password]");
        mysql_real_escape_string(pass, escpass);
        mysql_real_escape_string(Pname, escpname);
        GetPlayerIp(playerid, PIP, 50);
        format(Query, sizeof(Query), "UPDATE `playerinfo` SET `score` = '%d',`money` = '%d' WHERE `user` = '%s'", GetPlayerScore(playerid), GetPlayerMoney(playerid), escpname);
    mysql_query(Query);
        GameTextForPlayer(playerid, "~g~Registered", 2000, 3);
        SendClientMessage(playerid, 0x0000D9AA, "Registered and Logged into your account!");
        SetPVarInt(playerid, "Logged", 1);
    }
    return 1;
}
Also what's the need for select *? You don't need to use any of the information in the columns, so there's no reason to select it all, if you have a lot of columns this may induce performance issues on the server.

Edit: Too late , anyway the select * stuff still applies
What do you mean with the "select *"?

I'm still new to mysql, but if i can improve my knownledge, that always nice
Reply
#10

Okay well it's quite simple once you understand it, and it's not hard to explain so here we go

For example you have 5 columns

ID User Kills Deaths Party

Right, then you do a MySQL SELECT query, like this:

SELECT * FROM `table` WHERE ID = 1;

What this actually means is this:

SELECT ID,User,Kills,Deaths,Party FROM `table` WHERE ID = 1;

Now what's the point in getting MySQL to take all of that information from the table, when all you want to do is see how many rows are returned? What you're doing is causing added stress on the server to execute queries and grab loads of information you don't need.

In this case it is not an issue, because there is only five columns and that's not a lot, but some of the bigger servers may have many more columns, and then these SELECT * queries everywhere will really start to become noticeable, I've seen instances where the server actually hangs completely while processing the query.

Although I do believe MySQL plugin is threaded now, so the server won't hang, but you're still gathering a lot of information from the MySQL database that you're not going to use at all in that instance.

I hope this helps
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)