[SOLVED] Storing variables from MySQL
#1

Hello,

Can someone help me on this: (I've searched forums, already)

- I would like to set variables in the samp script with values which will be fetched from my mysql database.
Now the thing is, I don't know how to do it. I have some gamemodes with mysql who do it in a different way, but I don't understand their method, the way things are done.
Example
Код:
PlayerInfo[playerid][Level] = "mysql value here"
So that's what I want to do. Now this is what I've found:
Код:
SendQuery(szQuery);

  mysql_store_result();

  if (!mysql_num_rows())
   return SendNSMessage(playerid, COLOR_RED, "The password you supplied is invalid.");

  new index, line[256];

  mysql_fetch_row_format(line, "|");

  PlayerInfo[playerid][LoggedIn] = true;
  PlayerInfo[playerid][UserID] = strval(strtok(line, index, '|'));
  PlayerInfo[playerid][Level] = strval(strtok(line, index, '|'));
  PlayerInfo[playerid][Kills] = strval(strtok(line, index, '|'));

  //some queries here

  mysql_free_result();
Can someone explain to me what
Код:
mysql_fetch_row_format()
exactly does? And what is the purpose of strtok here? I searched strtok on wiki, but its very poorly explained as in what exactly everything is doing. Can't find any explanation of that index parameter. I'm not someone who wants to copy stuff from other scripts. I want to grasp and understand what is going on in the code..

Thanks.
Reply
#2

bump. some help would be greatly appreciated
Reply
#3

strtok splits a string. You have to use a separator to split the string.
And samp_mysql_row_format is used to define the separator of each column and save the formated result into the specified variable.


Example:

You use the separator | and have 3 columns in your MySQL table (col1, col2, col3)
Then you split the string into strings which contains the column data. You have to do that with strtok.
The Index variable is to save the index where the last split was done.
You call it 3 times (for each column) and you will get the data of each column as a separated string.
Код:
new Query[256];
new Row[256];
new String1[256];
new String2[256];
new String3[256];
new Index;
format(Query, sizeof(Query), "SELECT * FROM `your_table` LIMIT 1");
samp_mysql_query(Query);
samp_mysql_store_result();
samp_mysql_fetch_row_format(Row, "|");
String1 = strtok(Row, Index, "|"); // Content of the first column
String2 = strtok(Row, Index, "|"); // Content of the second column
String3 = strtok(Row, Index, "|"); // Content of the third column
I hope you understand that. :P
Reply
#4

why do you use LIMIT? what's it for?

+ why does it say in my example to free the result? to not keep it stored?

thx btw, Now I fully understand what strtok and the seperator is for, and especially the indes
Reply
#5

LIMIT 1 is to return only one row.
You can also use WHERE to check columns for their contents (e.g. only return the line of Player "Test").

Then you have to use
Код:
WHERE `column` = 'Content'
instead of
Код:
LIMIT 1
Replace 'column' with the columns name and 'Content' with the content you want to check.

But you may use LIMIT 1 with WHERE to make sure it only returns one row.
Or use a for-loop for each row and format the SQL query using format() and use LIMIT %d,1 (%d = Row Index starting at 0).

Код:
format(Query, sizeof(Query), "SELECT * FROM `your_table` LIMIT %d,1",RowNo);
RowNo is the number of the MySQL row you want to read (starting at 0).

Also read this:
SQL WHERE
SQL LIMIT


Quote:
Originally Posted by Faraday
+ why does it say in my example to free the result? to not keep it stored?
I don't know what it does, but I think it frees the result from the memory.
Reply
#6

Well,

thank you very much. It helped me alot. Now I can use this stuff in my scripts, since I know how they work.
It was very awesome of you to take your time to give me a straight-up answer!

Cheers, and if there's anything I can do to return the favor, just ask

Grts, Faraday
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)