open.mp forum
[Pawn] Help with mysql and data retrieving. - Printable Version

+- open.mp forum (https://forum.open.mp)
+-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
+--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
+--- Thread: [Pawn] Help with mysql and data retrieving. (/showthread.php?tid=1301)



Help with mysql and data retrieving. - cronoxliker - 2020-11-02

Hey guys, Im building a gamemode mostly for learning, but i got stuck in faction systems y was following a tutorial but was on an old version and im having struggle getting it done, if someone can explainme what im doing wrong here...



Code:
stock LoadFactions(/* arguments */) { new query[400]; for(new i = 0; i < MAX_FACTIONS; i) { mysql_tquery(query, "SELECT * FROM Factions WHERE ID = %d", i); new rows, fields; cache_get_data(rows, fields); if(rows) { Total_Factions_Created; cache_get_value_int(0,"id",Factions[i][ID]); cache_get_value_name(0,"name",Factions[i][Name]); cache_get_value_name(0,"type",Factions[i][Type]); cache_get_value_name(0,"Rank1",Factions[i][Rank1]); cache_get_value_name(0,"Rank2",Factions[i][Rank2]); cache_get_value_name(0,"Rank3",Factions[i][Rank3]); cache_get_value_name(0,"Rank1",Factions[i][Rank4]); } printf("se cargaron: %d facciones\n", Total_Factions_Created); } return 1; }



RE: Help with mysql and data retrieving. - Josh - 2020-11-02

If you're getting ALL of the factions just do a SELECT * FROM, there's no need to specify ID.

tquery will call a callback (which you specify) where you do cache_ function calls there, the plugin won't know what to do there because you have no cache active.


RE: Help with mysql and data retrieving. - cronoxliker - 2020-11-06

Any quick mysql data retrieving guide? im struggling with this, :/


RE: Help with mysql and data retrieving. - DeploYeR - 2020-11-06

// under OnGameModeInit

mysql_tquery(mysql_connection// change this to yours, "SELECT * FROM `Factions`", "LoadFactions");





forward LoadFactions();

public LoadFactions()

{

new rows, fields;

cache_get_data( rows, fields, your conn handle );



if( rows )

{

for( new i = 0; i < rows; i )

{

cache_get_value_int(i,"id",Factions[i][ID]);

cache_get_value_name(i,"name",Factions[i][Name]);

cache_get_value_name(i,"type",Factions[i][Type]);

cache_get_value_name(i,"Rank1",Factions[i][Rank1]);

cache_get_value_name(i,"Rank2",Factions[i][Rank2]);

cache_get_value_name(i,"Rank3",Factions[i][Rank3]);

cache_get_value_name(i,"Rank1",Factions[i][Rank4]);

}

}

return (true);

}


RE: Help with mysql and data retrieving. - cronoxliker - 2020-11-06

Cache_get_data looks like deprecated i swaped it with cache_get_value_index_int, now, how do i now if it works, i tried with printf but it returns nothing, any idea? thanks for the time guys!



my try to print the factions name=



CMD:printtest(playerid,params[])

{

new query[140];

for(new i = 0; i < MAX_FACTIONS; i)

{

format(query, sizeof(query),"SELECT * FROM Factions WHERE ID = %d",i);

mysql_tquery(g_SQL,query);

printf("%s\n", Factions[i][Name]);

}

return 1;

}

prints 30 empty rows (yes i know there are rows in the db)



thanks!


RE: Help with mysql and data retrieving. - Pinch - 2020-11-06

Use cache_get_value_name


RE: Help with mysql and data retrieving. - cronoxliker - 2020-11-07

(2020-11-02, 10:11 AM)Josh Wrote: If you're getting ALL of the factions just do a SELECT * FROM, there's no need to specify ID.

tquery will call a callback (which you specify) where you do cache_ function calls there, the plugin won't know what to do there because you have no cache active.



(2020-11-06, 07:49 PM)Pinch Wrote: Use cache_get_value_name



I get -error 035: argument type mismatch (argument 2)- if i just leave the rows as parameter then it will compile but still print 30 blank lines on /printtest, i can upload and edit but i cant retrieve the data...



if needed i can pass all the related code.


RE: Help with mysql and data retrieving. - Pinch - 2020-11-07

Because cache_get_value_index_int uses column's idx while cache_get_value_name uses literal string.

cache_get_value_name(row, "collumn_name", out);

Edit:
mysql_tquery(g_SQL,query);

I don't see a callback there....? Where were you using cache functions at all...?


RE: Help with mysql and data retrieving. - cronoxliker - 2020-11-10

(2020-11-07, 11:20 AM)Pinch Wrote: Because cache_get_value_index_int uses column's idx while cache_get_value_name uses literal string.



cache_get_value_name(row, "collumn_name", out);



Edit:

mysql_tquery(g_SQL,query);



I don't see a callback there....? Where were you using cache functions at all...?



this is my factions code, if u want i can provide the entire code, and the tutorial i read,?



Code:
enum faction { ID, //Facc ID Name[32], Type, //Type, criminal, law, legal no weapon, legal weapons. Rank1[32], //Rank name Rank2[32], //Rank name Rank3[32], //Rank name Rank4[32] //Rank name }; //enum initializing new Factions[MAX_FACTIONS][faction], Total_Factions_Created, facid[MAX_PLAYERS]; forward LoadFactions(); public LoadFactions() { new rows, fields; cache_get_value_index_int(rows,fields,g_SQL); if( rows ) { for( new i = 0; i < rows; i ) { cache_get_value_int(i,"id",Factions[i][ID]); cache_get_value_name(i,"name",Factions[i][Name]); cache_get_value_name(i,"type",Factions[i][Type]); cache_get_value_name(i,"Rank1",Factions[i][Rank1]); cache_get_value_name(i,"Rank2",Factions[i][Rank2]); cache_get_value_name(i,"Rank3",Factions[i][Rank3]); cache_get_value_name(i,"Rank1",Factions[i][Rank4]); } } return 1; } public OnGameModeInit() { new MySQLOpt: option_id = mysql_init_options(); mysql_set_option(option_id, AUTO_RECONNECT, true); // it automatically reconnects when loosing connection to mysql server g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id); // AUTO_RECONNECT is enabled for this connection handle only if (g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0) { print("MySQL connection failed. Server is shutting down."); SendRconCommand("exit"); // close the server if there is no connection return 1; } new query[103]; mysql_format(g_SQL, query, sizeof query,"SELECT * FROM Factions"); mysql_tquery(g_SQL, query, "LoadFactions"); print("MySQL connection is successful."); SetTimer("MoneyTimer", 1000, 1); return 1; } //working command CMD:fcreate(playerid,params[]) { new string[128], query[400], fname[32]; if(sscanf(params, "s[32]", fname)) return SendClientMessage(playerid,RED_COLOR,"USAGE: /fcreate factionname"); Total_Factions_Created; //----------------------------------- format(Factions[Total_Factions_Created][Name], 64,"%s", fname); Factions[Total_Factions_Created][ID] = Total_Factions_Created; //----------------------------------- format(string, sizeof(string),"Creaste la faccion %s(ID:%d).",Factions[Total_Factions_Created][Name],Factions[Total_Factions_Created][ID]); SendClientMessage(playerid, SYSTEM_COLOR, string); //----------------------------------- format(Factions[Total_Factions_Created][Rank1], 32, "Rank 1 - Lowest"); format(Factions[Total_Factions_Created][Rank2], 32, "Rank 2"); format(Factions[Total_Factions_Created][Rank3], 32, "Rank 3"); format(Factions[Total_Factions_Created][Rank4], 32, "Rank 4 - leader"); format(query, sizeof(query), "INSERT INTO `factions` (id, name, Rank1, Rank2, Rank3, Rank4) VALUES(%d,'%s','%s','%s','%s','%s')", Factions[Total_Factions_Created][ID], Factions[Total_Factions_Created][Name], Factions[Total_Factions_Created][Rank1], Factions[Total_Factions_Created][Rank2], Factions[Total_Factions_Created][Rank3], Factions[Total_Factions_Created][Rank4]); mysql_tquery(g_SQL,query); return 1; } //this one prints 30 empty lines CMD:printtest(playerid,params[]) { new query[140]; for(new i = 0; i < MAX_FACTIONS; i) { format(query, sizeof(query),"SELECT * FROM Factions WHERE ID = %d",i); mysql_tquery(g_SQL,query); printf("%s\n", Factions[i][Name]); } return 1; }



RE: Help with mysql and data retrieving. - Pinch - 2020-11-10

I don't wanna do everything from you because I see you understood nothing, I mean you literally have example scripts in the plugin's github.com repository...


RE: Help with mysql and data retrieving. - Expert* - 2020-11-10

U replaced cache_get_data with cache_get_value_index_int.

What you want is:

new rows;
cache_get_row_count( rows );

field count is irelevent to you in this case, so we don't have to know how many we have...

Also use this:
cache_get_value_name_int(i,"id",Factions[i][ID]);

Instead of:
cache_get_value_int(i,"id",Factions[i][ID]);