pBlueG / SA-MP-MySQL

MySQL plugin for San Andreas Multiplayer
BSD 3-Clause "New" or "Revised" License
197 stars 78 forks source link

Mysql IFNULL ? #185

Closed kutaskozla closed 6 years ago

kutaskozla commented 6 years ago

Why are the values downloaded 65535 0000-00-00 ?

vip date NOT NULL DEFAULT '0000-00-00',

in the database I have set the vip field 20 days ahead there is more data in the query, but they are downloaded well, there is only a somewhere IFNULL

new query[512];
mysql_format(g_SQL, query, sizeof(query), "SELECT IFNULL(DATEDIFF(viptime,NOW()),'-1'),IFNULL(viptime,'0000-00-00') WHERE `nick`='%e' LIMIT 1", PlayerName(playerid));          
mysql_tquery(g_SQL, query, "OnPlayerDataLoaded", "dd", playerid, g_MysqlRaceCheck[playerid]);

forward OnPlayerDataLoaded(playerid, race_check);
public OnPlayerDataLoaded(playerid, race_check)
{
    if (race_check != g_MysqlRaceCheck[playerid]) return Kick(playerid);

    if(cache_num_rows() > 0)
    {
        cache_get_value_int(0, "viptime", pData[playerid][vipDays]);
        cache_get_value(0, "viptime", pData[playerid][vipDate], 16);        
        printf("%d %s", pData[playerid][vipDaysLeft], pData[playerid][vipToDate]);  
        //pData[playerid][cacheID] = cache_save();
    }   
    return 1;
}
IstuntmanI commented 6 years ago

Try this:

new query[512];
/*changed this*/ mysql_format(g_SQL, query, sizeof(query), "SELECT IFNULL(DATEDIFF(viptime,NOW()),'-1') AS vipdays,IFNULL(viptime,'0000-00-00') AS viptime WHERE `nick`='%e' LIMIT 1", PlayerName(playerid));           
mysql_tquery(g_SQL, query, "OnPlayerDataLoaded", "dd", playerid, g_MysqlRaceCheck[playerid]);

forward OnPlayerDataLoaded(playerid, race_check);
public OnPlayerDataLoaded(playerid, race_check)
{
    if (race_check != g_MysqlRaceCheck[playerid]) return Kick(playerid);

    if(cache_num_rows() > 0)
    {
        /*and this*/ cache_get_value_int(0, "vipdays", pData[playerid][vipDaysLeft]);
        cache_get_value(0, "viptime", pData[playerid][vipDate], 16);        
        /*and this*/ printf("%d %s", pData[playerid][vipDaysLeft], pData[playerid][vipToDate]); 
        //pData[playerid][cacheID] = cache_save();
    }   
    return 1;
}
  1. You didn't give a name to the selected rows.
  2. You tried cache_get_value_int from viptime, which is of type DATE I guess, also tried on the same column with cache_get_value.
  3. You loaded into pData[playerid][vipDays] but you printed pData[playerid][vipDaysLeft].
  4. Your code probably outputs warnings/errors in the mysql log. Enable it with mysql_log( ERROR | WARNING ) and check it in the future!

Not sure if it will work as you want, but that's better than your code.

Also, as me and @maddinat0r said tons of times: This isn't an issue with the MySQL Plugin! Post your questions on the forum! GitHub Issues is for project's issues/suggestions, not for your regular questions.