Reading from database
#1

For some reason I cannot read from database properly using this script...
Please advise.

pawn Код:
public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerCameraPos(playerid, -127.6169, 1209.2051, 19.7422);
    SetPlayerCameraLookAt(playerid, -127.3276, 1188.2688, 19.6260);
    new playername[30], str[100], pass[30];
    GetPlayerName(playerid, playername, 30);
    new DB:playerlog=db_open("Playerlog.db");
    format(str, sizeof(str), "SELECT Password FROM 'Players' WHERE Name = '%s'", playername);
    new DBResult:result=db_query(playerlog, str);
    db_get_field_assoc(result, "Password", pass, sizeof(pass));
    SendClientMessage(playerid, 0x000000, pass);
    db_close(playerlog);
    return 1;
}
Reply
#2

Do not bump your topic within 48 hours. Not only is it unnecessary, it's against the rules.

As for your problem, can you give more information? What do you mean by you can't read from the database properly? Does the string you're storing the password in have anything in it or is it null? Are you sure you're connected to the database? Are you using the correct table name?

Your problem is most likely because of improper SQL syntax. You use ` around table names.
pawn Код:
format(str, sizeof(str), "SELECT Password FROM `Players` WHERE Name = '%s'", playername);
Reply
#3

When I try to print the password on my screen through SendClientMessage, only an empty row appears in the chatlog. I tried wil full and empty strings, all show the same. I've made the string to be empty by default, and I am sure the table and database are existing and correct. I also have text-type data in the specified field.
I think I am not using these lines properly:
pawn Код:
format(str, sizeof(str), "SELECT Password FROM `Players` WHERE Name = '%s'", playername);
new DBResult:result=db_query(playerlog, str);
db_get_field_assoc(result, "Password", pass, sizeof(pass));
Probably something wrong with the db_query and db_get_field_assoc...

And sorry for bumping, I think I must go read the rules.
Reply
#4

If you decide to use the grave symbol (`), you need to wrap it around fields too.

pawn Код:
format(str, sizeof(str), "SELECT `Password` FROM `Players` WHERE `Name` = '%s'", playername);
Reply
#5

Alright, thank you to everyone who helped, it worked!

P.S. I found a small mistake in my database (tooth) and I fixed it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)