SA-MP Forums Archive
Quick question on mysql and filterscript! - 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: Quick question on mysql and filterscript! (/showthread.php?tid=469429)



Quick question on mysql and filterscript! - Jerm - 13.10.2013

So if I am using a gamemode which has an SQL database storing all the player information and that includes money.

I want to know what key things id need to add to a filter script in order to access that information.
For example I cant make a filterscript that affects money unless I can alter and access the data from the gamemode and the sql its using.

Thanks in advance


Re: Quick question on mysql and filterscript! - showdown - 13.10.2013

If I understand you correctly, I think you need to do the following:

at the top of the filterscript you need to
Код:
#include <a_mysql>
then you would connect to the mysql server and access the database the same way the game mode accesses it

Код:
GetMoney(name[])
{
mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS);

new Query[256];
format(Query, 256, "SELECT * FROM playerdata WHERE username='%s'", name);
mysql_query(Query);
mysql_store_result();

new nMoneyAmount;

while(mysql_fetch_row_format(Query,"|"))
{
	mysql_fetch_field_row(nMoneyAmount, "money"); //nMoneyAmount will store the retrieved variable, in this case the players money, and "money" is the name of the row in the database
}

return nMoneyAmount;
}
Let me know if you need anything here clarified.


Re: Quick question on mysql and filterscript! - Jerm - 13.10.2013

Okay so I am trying to do as you said (Thanks for the help by the way I will try to +rep if it is even possible for me yet):
Код:
#include <a_samp>
#include <a_mysql>

GetMoney(name[])
{
	mysql_connect("127.0.0.1", "root", "freshdatabase", "");
	new Query[256];
	format(Query, 256, "SELECT * FROM accounts WHERE username='%s'", name);
	mysql_query(Query);
	mysql_store_result();

	new nMoneyAmount;
	while(mysql_fetch_row_format(Query,"|"))
	{
		mysql_fetch_field_row(nMoneyAmount, "Money"); // THIS IS LINE 15!
	return nMoneyAmount;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new string[48], pMoney, pName[24];
	pName[24] = GetPlayerName(playerid); // THIS IS LINE 23
	pMoney = GetMoney(pName[24]); // THIS IS LINE 24
	if(!strcmp(cmdtext, "/mysqltest", true))
	{
		format (string, sizeof(string), "%d", pMoney);
		SendClientMessage(playerid, 0xC2A2DAAA, string);
	}
	return 0;
}
Basically its a command that I can type in and it will tell me how much cash I have on me.
Using this just as a way to learn the MySQL stuff before actually creating useful commands.

Here is where I am having issues though. Maybe you can spot what it is really quick. I marked the three lines above that belong to these errors.
Код:
C:\Users\owner\Documents\server\Server\filterscripts\MYSQLTEST.pwn(15) : error 035: argument type mismatch (argument 1)
C:\Users\owner\Documents\server\Server\filterscripts\MYSQLTEST.pwn(23) : error 032: array index out of bounds (variable "pName")
C:\Users\owner\Documents\server\Server\filterscripts\MYSQLTEST.pwn(24) : error 032: array index out of bounds (variable "pName")
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


3 Errors.



Re: Quick question on mysql and filterscript! - Jerm - 13.10.2013

Okay I fixed two of the errors I was getting by creating a stock (saw a similar post to mine):
Код:
#include <a_samp>
#include <a_mysql>

GetMoney(name[])
{
	mysql_connect("127.0.0.1", "root", "freshdatabase", "");
	new Query[256];
	format(Query, 256, "SELECT * FROM accounts WHERE username='%s'", name);
	mysql_query(Query);
	mysql_store_result();

	new nMoneyAmount;
	while(mysql_fetch_row_format(Query,"|"))
	{
		mysql_fetch_field_row(nMoneyAmount, "Money"); // THIS IS LINE 15!
	}
	return nMoneyAmount;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new string[48], pMoney;
	pMoney = GetMoney(pName(playerid));
	if(!strcmp(cmdtext, "/mysqltest", true))
	{
		format (string, sizeof(string), "%d", pMoney);
		SendClientMessage(playerid, 0xC2A2DAAA, string);
	}
	return 0;
}

public OnFilterScriptInit()
{

}

stock pName(playerid)
{
    new
        iName[MAX_PLAYER_NAME];

    GetPlayerName(playerid, iName, sizeof(iName));
    return iName;
}
Still getting the one error though:
Код:
C:\Users\owner\Documents\server\Server\filterscripts\MYSQLTEST.pwn(15) : error 035: argument type mismatch (argument 1)
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


1 Error.



Re: Quick question on mysql and filterscript! - Jerm - 13.10.2013

Okay I got it to compile with zero errors and warning:

Код:
#include <a_samp>
#include <a_mysql>

GetMoney(name[])
{
	mysql_connect("127.0.0.1", "root", "freshdatabase", "");
	new Query[256];
	format(Query, 256, "SELECT * FROM accounts WHERE Username='%s'", name);
	mysql_query(Query);
	mysql_store_result();

	new nMoneyAmount[48];
	while(mysql_fetch_row_format(Query,"|"))
	{
		mysql_fetch_field_row(nMoneyAmount, "Money"); //nMoneyAmount will store the retrieved variable, in this case the players money, and "money" is the name of the row in the database
	}
	return nMoneyAmount;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new string[48], pMoney[48], checker[48];
	checker = pName(playerid);
	pMoney = GetMoney(checker);
	if(!strcmp(cmdtext, "/mysqltest", true))
	{
		format (string, sizeof(string), "%d", pMoney);
		SendClientMessage(playerid, 0xC2A2DAAA, string);
	}
	return 0;
}

/*public OnFilterScriptInit()
{

}*/

stock pName(playerid)
{
    new
        iName[MAX_PLAYER_NAME];

    GetPlayerName(playerid, iName, sizeof(iName));
    return iName;
}
Not sure if I did everything correctly though.

When loading the script in game and trying the command it does nothing the first time, then after that it works fine but always returns a zero instead of the money I have on me.

The reason it's returning a zero might have something to do with this error I get when using the command:
Код:
[13/10/2013 06:01:18] Query error (errorid: 1065, error: Query was empty, callback: NULL, connectionHandle: 1)
[13/10/2013 06:01:19] Query error (errorid: 1065, error: Query was empty, callback: NULL, connectionHandle: 1)
[13/10/2013 06:01:21] Query error (errorid: 1065, error: Query was empty, callback: NULL, connectionHandle: 1)



Re: Quick question on mysql and filterscript! - Jerm - 14.10.2013

Bumping this!


Re: Quick question on mysql and filterscript! - Jerm - 15.10.2013

Bump I know someone knows this stuff like it's nothing!


Re: Quick question on mysql and filterscript! - [LCK]Chris - 15.10.2013

This might help.

Quote:

#include <a_samp>
#include <a_mysql>

GetMoney(name[])
{
mysql_connect("127.0.0.1", "root", "freshdatabase", "");
new Query[64];
format(Query, 256, "SELECT * FROM accounts WHERE Username='%s'", name);
mysql_query(Query);
mysql_store_result();

new nMoneyAmount[48];
while(mysql_fetch_row_format(Query,"|"))
{
mysql_fetch_field_row(nMoneyAmount, "Money");
}
return strval(nMoneyAmount);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
new string[48], pMoney;

pMoney = GetMoney(pName(playerid));

if(!strcmp(cmdtext, "/mysqltest", true))
{
format(string, 48, "Your Money: %i", pMoney);
SendClientMessage(playerid, 0xC2A2DAAA, string);
return true;
}
return false;
}

stock pName(playerid)
{
new
iName[MAX_PLAYER_NAME];

GetPlayerName(playerid, iName, sizeof(iName));
return iName;
}




Re: Quick question on mysql and filterscript! - Jerm - 15.10.2013

Thanks but when I ran that script and typed the command it just closed my server instantly.. not sure what caused that.


Re: Quick question on mysql and filterscript! - [LCK]Chris - 15.10.2013

oh the string size
Quote:

format(Query, 64, "SELECT * FROM accounts WHERE Username='%s'", name);