[HELP] Getting data from MySQL database ????
#1

Could someone help me and tell me what is wrong here:
(Windows sa-mp server with MySQL)


public OnPlayerLogin(playerid)
{
new query[256];
new pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
mysql_real_escape_string(pname, pname);
MySQLCheck();
format(query, sizeof(query), "SELECT * FROM `"TABLENAME"` WHERE account_name = '%s'", pname);
$result = mysql_query(query, CONNECT_THREAD_ID);
mysql_store_result();
while ($row = mysql_fetch_row($result)){

PlayerInfo[playerid][account_id] = $row[account_id];
PlayerInfo[playerid][account_name] = $row[account_name];
}

}


I bolded the potential problem areas. I am not sure, however, how to change this to make it work correctly. Please help!! Thanks!


Error report:

Quote:

pwn(2803) : error 029: invalid expression, assumed zero
pwn(2803) : error 017: undefined symbol "row"
pwn(2803) : error 029: invalid expression, assumed zero
pwn(2803) : fatal error 107: too many error messages on one line

Line 2803 is "while ($row = mysql_fetch_row($result)){"
Reply
#2

You are using PHP in pawno, the two are different in alot of ways. You need to use pawno coding, not PHP
Reply
#3

What is the pwn code to do this?
Reply
#4

Could some show me an example of how to SELECT?
Reply
#5

only the bold area is from php, its a bit different in pawn;

This is how i have it;

pawn Код:
format(query, 50, "SELECT * FROM `accounts` WHERE `account_id`='%d'",id);
    mysql_query(query);
    mysql_store_result();

    mysql_fetch_row_format(row,"|");
    explode(result,row, "|");

    PlayerInfo[playerid][hID] = strval(result[0]);
Reply
#6

So does that basically get all of the results (SELECT *) and divide them up with a "|" -- and then you remove the "|" for each row?

Also, how do you define your variables?

.pwn(2804) : error 017: undefined symbol "row"
.pwn(2805) : error 017: undefined symbol "explode"
.pwn(2807) : error 017: undefined symbol "result"
Reply
#7

I've tried to figure it out based on what you provided here, but the explode(result, row, "|"); is really confusing me.

What is that exactly? Can you show me the code for that if you don't mind? Specifically the explode function.

Here is what I have now:


new query[256];
new row[256];
new result[256];
new pname[MAX_PLAYER_NAME];
GetPlayerName(playerid,pname,sizeof(pname));
mysql_real_escape_string(pname,pname);
MySQLCheck();
format(query,sizeof(query),"SELECT * FROM `"TABLENAME"` WHERE account_name = '%s'",pname);
mysql_query(query);
mysql_store_result();

mysql_fetch_row_format(row, "|");
explode(result, row, "|");

PlayerInfo[playerid][account_id] = strval(result[0]);
PlayerInfo[playerid][account_name] = strval(result[1]);
PlayerInfo[playerid][account_password] = strval(result[2]);


.pwn(2807) : error 017: undefined symbol "explode"


Does that look good (besides the explode not working)?
Reply
#8

I suggest you using sscanf!
pawn Код:
stock LoadPlayerData( playerid )
{
    new
        line[ 1024 ] ,
        string[ 105 ] ,
        query[ 68 ];
    mysql_real_escape_string( PlayerInfo[ playerid ][ Name ] , PlayerInfo[ playerid ][ Name ] );
    format( query , sizeof( query ) , "SELECT * FROM `players` WHERE Nickname = '%s'" , PlayerInfo[ playerid ][ Name ] );
    mysql_query( query );
    mysql_store_result( );
    mysql_fetch_row_format( line , "|" );
    sscanf( line , "p|dsssssdddddddddddddddddddddddfffss" , PlayerInfo[ playerid ][ MySQLID ] , PlayerInfo[ playerid ][ Name ] , PlayerInfo[ playerid ][ Password ] , PlayerInfo[ playerid ][ EMail ] , PlayerInfo[ playerid ][ RegIP ] , PlayerInfo[ playerid ][ IP ] , PlayerInfo[ playerid ][ Kills ] , PlayerInfo[ playerid ][ Deaths ] , PlayerInfo[ playerid ][ Cash ] , PlayerInfo[ playerid ][ Bank ] ,
                                                                PlayerInfo[ playerid ][ Level ] , PlayerInfo[ playerid ][ VIP ] , PlayerInfo[ playerid ][ Muted ] , PlayerInfo[ playerid ][ MuteMinutes ] , PlayerInfo[ playerid ][ Jailed ] , PlayerInfo[ playerid ][ JailMinutes ] , PlayerInfo[ playerid ][ Frozen ] , PlayerInfo[ playerid ][ FreezeMinutes ] , PlayerInfo[ playerid ][ Caged ] ,
                                                                PlayerInfo[ playerid ][ CageMinutes ] , PlayerInfo[ playerid ][ Donated ] , PlayerInfo[ playerid ][ Skin ] , PlayerInfo[ playerid ][ MsgLevel ] , PlayerInfo[ playerid ][ AdminHidden ] , PlayerInfo[ playerid ][ Hours ] , PlayerInfo[ playerid ][ Minutes ] , PlayerInfo[ playerid ][ Seconds ] , PlayerInfo[ playerid ][ SavedPos ] ,
                                                                PlayerInfo[ playerid ][ SavedPos0 ] , PlayerInfo[ playerid ][ SavedPos1 ] , PlayerInfo[ playerid ][ SavedPos2 ] , PlayerInfo[ playerid ][ RegisterDate ] , PlayerInfo[ playerid ][ LastOnline ] );
    mysql_free_result( );
You will have to replace some things as it's from my gamemode and it won't work with yours. I also suggest you to read pawn-lang.pdf.
Reply
#9

What does this mean: "p|dsssssdddddddddddddddddddddddfffss" ??

And for sscanf to work, do I need to define it in my code?
Reply
#10

For the actual code see this page: https://sampwiki.blast.hk/wiki/Sscanf
For the usage of sscanf see this page: https://sampwiki.blast.hk/wiki/Fast_Commands#sscanf

And for the "p|dsssssdddddddddddddddddddddddfffss" part. I have a variable, new line[ 1024 ];Then I execute the query etc. After that I use mysql_fetch_row_format( line , "|" ); to store the results in the line variable, seperating every field with a "|", then with the sscanf code I use this:
Код:
"p|dsssssdddddddddddddddddddddddfffss"
p| tells sscanf that is has to look for the | character. Then I use dssssdddd etc. to tells sscanf that the first characters before the first | are integers. As d in the sscanf code stands for an integer. Then I use s to tell sscanf the next characters are a string.

"151|stringishere|"
That would require this code:
Код:
"p|ds"
But for more help I redirect you to the wiki, it has more examples!


Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)