Fetching a data from mySQL
#1

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!!
Reply
#2

What's your mysql version.
Reply
#3

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.
Reply
#4

My mySQL version is R41-4

@jlalt Uhm It's really confusing, can you reply again when you're on a PC?
Reply
#5

If I got to PC and this thread wasn't answered yet sure, I would re explain.
Reply
#6

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
Reply
#7

Код:
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.
Reply
#8

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.
Reply
#9

PHP код:
new Cache:resultquery[70];
mysql_format(g_SQLquerysizeof query"SELECT `skin` FROM `players` WHERE `id` = %d LIMIT 1"Player[playerid][ID]);
result mysql_query(g_SQLquery);
if(
cache_num_rows())
{
    
cache_get_value_int(0"skin"Player[playerid][Skin]);
    
SetPlayerSkin(playeridPlayer[playerid][Skin]);
}
else 
SendClientMessage(playerid, -1"Nothing found");
cache_delete(result); 
Reply
#10

Quote:
Originally Posted by oMa37
Посмотреть сообщение
PHP код:
new Cache:resultquery[70];
mysql_format(g_SQLquerysizeof query"SELECT `skin` FROM `players` WHERE `id` = %d LIMIT 1"Player[playerid][ID]);
result mysql_query(g_SQLquery);
if(
cache_num_rows())
{
    
cache_get_value_int(0"skin"Player[playerid][Skin]);
    
SetPlayerSkin(playeridPlayer[playerid][Skin]);
}
else 
SendClientMessage(playerid, -1"Nothing found");
cache_delete(result); 
Worked, thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)