[MySQL] Insert array
#1

Hi all,

I was wondering how to insert an array into a database. I believe you can't do that directly, or can you? In php you got this function, serialize, which converts an array into a string. With unserialize you can turn it back to an array. But this function is no native function in pawn so it's pretty useless.

I have been thinking about putting all the values of an array into floats and store them in the database. But that gives me more problems:
1. It takes a long time to perform;
2. It makes the database messy as well as the code.

I have also tried to store it as TEXT into the database. But then again you can't use Float:array[][]'s.

The real problem is that every 1500 miliseconds a script collects all the playerpositions (just the X's and Y's) and puts them in an array. Next those variables should be stored in the database. Third, a php script will take them from the database and put red markers on a map according to the coordinates of players. But I don't know what the best way is to store such an array in a database. Currently I got something like this
Код:
new i,
	Query[1024];
	
for(i = 0; i < MAX_PLAYERS; i++){
	format(Query, sizeof(Query), "UPDATE `samp`.`locations` SET `X` = '%f',`Y` = '%f' WHERE `locations`.`playerid` =%d;", positions[i][0], positions[i][1], i);
	mysql_query(Query);
}
But that makes the server lagg because it takes too long.

If anybody has an idea of an efficient way to solve this I would like to hear about it.

Thanks in advance.
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
Simple, don't use SQL to transfer data between things - it's not disigned for that. If you want to get data from PAWN to PHP use a file, write to it then read the data out from it at the other end. You can customise the file access code to better suit your needs.
Allright, but how to write an array to a file? I guess I'll start putting all the information of the array in single floats in the file? That will take a long time. Also, I just think there must be another way to do this simple and efficient.
Reply
#3

Quote:
Originally Posted by ******
Посмотреть сообщение
Are you sure it will take a long time? Have you tried it? And why is that not simple?

You could also format multiple elements together to increase line length to decrease file line writes.
I certainly have tried it, with MAX_PLAYERS * 2 = 100, and it laggs the server for a moment. I have also allready tried to format multiple elements together as a string like this
Код:
new i,
	string[MAX_PLAYERS];
for(i = 0; i <= MAX_PLAYERS; i++){
	string[i] = positions[i][0]
}
printf(string);
But the printed string is allways an empty line, however when I format the string like this
Код:
new i,
	string[MAX_PLAYERS];
string[0] = positions[0][0]
string[1] = positions[1][0]
string[2] = positions[2][0]
...
string[48] = positions[48][0]
string[49] = positions[49][0]
string[50] = positions[50][0]
printf(string);
It'll work fine. Now, to do this for 50 times manually seems really ugly, and you must agree with me on that. When I will upgrade to 150, 250 or even 500 players that won't be nice code, not?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)