Fetching a data from mySQL -
Fratello - 27.09.2017
Hello there!
I recently started working with mySQL querys. And I'm facing a problem. I need to fetch a data from my database (player's skin) but I don't know how exactly.
My player skin saves perfectly, and here is the code:
Код:
new playerSkin = GetPlayerSkin(playerid);
new query[70];
mysql_format(g_SQL, query, sizeof query, "UPDATE `players` SET `skin` = %d WHERE `id` = %d LIMIT 1", playerSkin, Player[playerid][ID]);
mysql_tquery(g_SQL, query);
I don't know what should I do to make it work, I tried adding a _tempskin to storage somewhere fetched data and later on I can set the skin but it returns CJ skin.
Код:
new query[70];
new _tempskin;
mysql_format(g_SQL, query, sizeof query, "SELECT FROM `players` WHERE `skin` = %d LIMIT 1", _tempskin);
mysql_tquery(g_SQL, query);
SetPlayerSkin(playerid, _tempskin);
Thanks!!
Re: Fetching a data from mySQL -
AndreiWow - 27.09.2017
What's your mysql version.
Re: Fetching a data from mySQL -
jlalt - 27.09.2017
First you need to learn SQL select syntax.
Its: select `field1`, `field2` or * from `table` where `smth` = 'value'
Ex: select `skin` from `players` where `id` = '%d'
And then will need to use mysql_query or tquery
And story the cache Id or go to tquery finish callback
And then use
https://sampwiki.blast.hk/wiki/MySQL/R40...value_name_int
To get skin value.
Sorry for a bit shit explain I'm on phone.
Re: Fetching a data from mySQL -
Fratello - 27.09.2017
My mySQL version is
R41-4
@jlalt Uhm It's really confusing, can you reply again when you're on a PC?
Re: Fetching a data from mySQL -
jlalt - 27.09.2017
If I got to PC and this thread wasn't answered yet sure, I would re explain.
Re: Fetching a data from mySQL -
raydx - 27.09.2017
You have to change query:
Код:
mysql_format(g_SQL, query, sizeof query, "SELECT FROM `players` WHERE `skin` = %d LIMIT 1", _tempskin);
Correct:
Код:
mysql_format(g_SQL, query, sizeof query, "SELECT skin FROM `players` WHERE `id` = %d LIMIT 1", Player[playerid][ID]);
After that, you can fetch like that:
Код:
if(cache_num_rows())
{
cache_get_value_index_int(0, 0, _tempskin);
}
First 0 = row
Second 0 = column
Also you can't use mysql_tquery like that.
tquery = threaded queries, you have to make callback for them.
In your case use mysql_query (non threded query).
And read about cleaning cache in non threaded queries -
https://sampwiki.blast.hk/wiki/MySQL/R40#mysql_query
Re: Fetching a data from mySQL -
whadez - 27.09.2017
Код:
cache_get_value_int(0, "skin", pInfo[playerid][pSkin]);
"tquery = threaded queries, you have to make callback for them."
This statement is not true.
I've written my own gamemode which contains 38.000 lines with tons of dynamic systems using the latest mysql version, and I'm pretty sure I've never used normal query, it's useless nowadays, you can wait that couple of milliseconds to get the values the user wont notice it, and performance-wise it's more clever to do so.
You should only use 'pquery' at loading tons of data, f.e loading houses, cars, when your gamemode inits.
For tquery without callback just use this.
Код:
mysql_tquery(g_SQL, query, "", "");
Also if you want to have an 'id' value, go into your player table, make a new column, name it 'id', and put it as UNIQUE - AUTO INCREMENT, then your players will be indexed, and you can verify them based on their 'id'. If you are not sure what auto increment is, ****** it up, you'll understand, it's an unique id what other users cant have, unless you change it for them.
Re: Fetching a data from mySQL -
Fratello - 27.09.2017
Err, I'll look up for explanation about tquery, cleaning cache ... later on!
I'm not sure if I get this good, but it should work like this?
Код:
new query[70];
new _tempskin;
mysql_format(g_SQL, query, sizeof query, "SELECT `skin` FROM `players` WHERE `id` = %d LIMIT 1", Player[playerid][ID]);
if(cache_num_rows())
{
cache_get_value_int(0, "skin", Player[playerid][Skin]);
}
SetPlayerSkin(playerid, _tempskin);
I've tried to put SetPlayerSkin in chache_num_rows too. And yes I'm 100% sure script fatches player's skin. I've checked it.
Re: Fetching a data from mySQL -
oMa37 - 27.09.2017
PHP код:
new Cache:result, query[70];
mysql_format(g_SQL, query, sizeof query, "SELECT `skin` FROM `players` WHERE `id` = %d LIMIT 1", Player[playerid][ID]);
result = mysql_query(g_SQL, query);
if(cache_num_rows())
{
cache_get_value_int(0, "skin", Player[playerid][Skin]);
SetPlayerSkin(playerid, Player[playerid][Skin]);
}
else SendClientMessage(playerid, -1, "Nothing found");
cache_delete(result);
Re: Fetching a data from mySQL -
Fratello - 27.09.2017
Quote:
Originally Posted by oMa37
PHP код:
new Cache:result, query[70];
mysql_format(g_SQL, query, sizeof query, "SELECT `skin` FROM `players` WHERE `id` = %d LIMIT 1", Player[playerid][ID]);
result = mysql_query(g_SQL, query);
if(cache_num_rows())
{
cache_get_value_int(0, "skin", Player[playerid][Skin]);
SetPlayerSkin(playerid, Player[playerid][Skin]);
}
else SendClientMessage(playerid, -1, "Nothing found");
cache_delete(result);
|
Worked, thanks!