CallBackFunction MYSQL cached
#1

Hello!

I'm trying to call faction name from the table 'faction'.

it's called 'factionname'. So I'm trying to assign factionname to Faction[playerid][factionname].

However it doesn't seem to work.

PHP Code:
<div style="margin:20px; margin-top:5px">
<
div class="smallfont" style="margin-bottom:2px">PHP код:</div>
<
div class="alt2" dir="ltr" style="         margin: 0px;         padding: 6px;         border: 1px solid #C2C2C2;         width: 800px;         height: 178px;         text-align: left;         overflow: auto"> <code style="white-space:nowrap"> <!-- php buffer start --><code><span style="color: #000000"> <span style="color: #0000BB">forward callBackFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">playerid</span><span style="color: #007700">); <br/>public </span><span style="color: #0000BB">callBackFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">playerid</span><span style="color: #007700">) <br/>{ <br/>    <br/>    </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">] = </span><span style="color: #0000BB">cache_insert_id</span><span style="color: #007700">(); <br/>    </span><span style="color: #0000BB">cache_get_field_content</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #DD0000">"factionname"</span><span style="color: #007700">,</span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">FactionName</span><span style="color: #007700">]); <br/>    </span><span style="color: #0000BB">ShowPlayerDialog</span><span style="color: #007700">(</span><span style="color: #0000BB">playerid</span><span style="color: #007700">, </span><span style="color: #0000BB">factiontyped</span><span style="color: #007700">, </span><span style="color: #0000BB">DIALOG_STYLE_LIST</span><span style="color: #007700">, </span><span style="color: #DD0000">"FACTION CREATION"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Type 0(Drugs)\nType 1(Weapons)"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Choose"</span><span style="color: #007700">,</span><span style="color: #DD0000">"Cancel"</span><span style="color: #007700">); <br/>    </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[INFO] New faction created. Database ID: [%d]"</span><span style="color: #007700">, </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">]); <br/>}  <br/></span><span style="color: #0000BB"></span> </span> </code><!-- php buffer end --> </code> </div>
</
div>I'm trying to access Faction[playerid][Factionname] to a individual player like this.
<div style="margin:20px; margin-top:5px">
<div class="smallfont" style="margin-bottom:2px">PHP код:</div>
<div class="alt2" dir="ltr" style="         margin: 0px;         padding: 6px;         border: 1px solid #C2C2C2;         width: 800px;         height: 50px;         text-align: left;         overflow: auto"> <code style="white-space:nowrap"> <!-- php buffer start --><code><span style="color: #000000"> <span style="color: #0000BB"></span><span style="color: #007700">[</span><span style="color: #0000BB">PHP</span><span style="color: #007700">]</span><span style="color: #0000BB">mysql_format</span><span style="color: #007700">(</span><span style="color: #0000BB">mysql</span><span style="color: #007700">,</span><span style="color: #0000BB">query</span><span style="color: #007700">,</span><span style="color: #0000BB">sizeof</span><span style="color: #007700">(</span><span style="color: #0000BB">query</span><span style="color: #007700">), </span><span style="color: #DD0000">"UPDATE `accounts` SET `factionname` = %e `factionid` = %d WHERE ID = %d LIMIT 1"</span><span style="color: #007700">, </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">FactionName</span><span style="color: #007700">], </span><span style="color: #0000BB">Faction</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">], </span><span style="color: #0000BB">Player</span><span style="color: #007700">[</span><span style="color: #0000BB">playerid</span><span style="color: #007700">][</span><span style="color: #0000BB">ID</span><span style="color: #007700">]); <br/>                </span><span style="color: #0000BB">mysql_tquery</span><span style="color: #007700">(</span><span style="color: #0000BB">mysql</span><span style="color: #007700">,</span><span style="color: #0000BB">query</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">,</span><span style="color: #DD0000">""</span><span style="color: #007700">);  <br/></span><span style="color: #0000BB"></span> </span> </code><!-- php buffer end --> </code> </div>
</div> 
Reply
#2

BUMP, need help still!
Reply
#3

If you are INSERTING a new column, you cannot read with the same query. Inserting only writes the result, not store it.

And you can just write the faction name to player when you use the mysql_tquery.

pawn Код:
mysql_tquery("INSERT INTO...", "callBackFunction", "i", playerid);
format(Faction[playerid][FactionName], SIZE, name);
Reply
#4

A bit confused. So what I'm trying to achieve is.
I have a /createfaction -> Which stores 'factionname' and 'factionid' under my faction table. Using INSERT INTO.
Simliarly, I also have 'factionname' and 'factionid' under my accounts table in where I'm trying to link a individual who created that faction on it.

So upon /createfaction -> I use INSERT INTO and put faction name as the inputtext in my dialog.
Similarly I want to be able to use 'UPDATE' or any mysql function and add the same inputtext under my `accounts` table at `factionname` and assign it a `factionid` similary I did under `faction` table. The way I'm assinging faction id is using cache_insert_id as I'm using Auto Increment.
So I thought using callbackfunction would let me do it.
But it isn't working.
Reply
#5

Also I'm trying to assign a player to a faction name using Player[playerid][FactionName] = inputtext, which returns me a error saying
array sizes do not match, or destination array is too small
Reply
#6

Can't I just assign Player[playerid][FactionName] = inputtext[120] under my dialogresponse.
Then call Player[playerid][FactionName] to UPDATE my `factionname` under my `account` table. i tried this and tried priting Player[playerid][FactionName] it prints me empty string. What am I doing wrong?

PHP код:
case factionnamed:
        {
            new 
query[500];
            
mysql_format(mysql,query,sizeof(query), "INSERT INTO `faction` (`factionname`) VALUES ('%e')",inputtext);
            
mysql_tquery(mysqlquery"callBackFunction""i",playerid);
            
Player[playerid[FactionName] = inputtext[120];
            
mysql_format(mysql,query,sizeof(query), "UPDATE `accounts` SET `factionname` = %e `factionid` = %d WHERE ID %d LIMIT 1"Player[playerid][FactionName], Faction[playerid][ID], Player[playerid][ID]);
            
mysql_tquery(mysql,query,"","");
            
         } 
Under my command where i assign dialog is like this
PHP код:
CMD:createfaction(playerid,params[])
{
    if(
Player[playerid][Admin] < 4) return SendClientMessage(playeridCOLOR_GREY"You must be an adminstrator to use this command");
    
ShowPlayerDialog(playeridfactionnamedDIALOG_STYLE_INPUT"Faction Creation""Enter the faction name""Next","Cancel");
    return 
1;

Under enums, I have
PHP код:
enum PlayerData
{
    
ID,
    
Name[MAX_PLAYER_NAME],
    
Password[129],
    
IP[16],
    
Admin,
    
VIP,
    
FactionID,
    
FactionName[200],
    
Money,
    
Float:posX,
    
Float:posY,
    
Float:posZ,
    
Float:posA
};
new 
Player[MAX_PLAYERS][PlayerData]; 
PHP код:
enum FactionData
{
    
ID,
    
factionname[200],
    
factiontype
};
new 
Faction[MAX_PLAYERS][FactionData]; 
Reply
#7

Your queries are completly wrong, no use of "," at places, no "`" to enclose columns and so one...
I suggest you to go through a basic syntax tutorial on SQL. Can be found anywhere on ******.

Here is the correct way and you don't need to use a callback for that, because mysql_tquery returns 1 or 0 for success and fail.
pawn Код:
case factionnamed:
{
    new bool:success = true;

    format(Player[playerid[FactionName], 120, inputtext);

    new query[200];
    mysql_format(mysql, query, sizeof (query), "INSERT INTO `faction` (`factionname`) VALUES ('%e')", inputtext);
    if (mysql_tquery(mysql, query) == 0)
        success = false;

    mysql_format(mysql, query, sizeof (query), "UPDATE `accounts` SET `factionname` = '%e', `factionid` = %d WHERE `ID` =  %d LIMIT 1", Player[playerid][FactionName], Faction[playerid][ID], Player[playerid][ID]);
    if (mysql_tquery(mysql, query) == 0)
        success = false;

    if (success)
    {
        // Every thing went well
        ShowPlayerDialog(playerid, factiontyped, DIALOG_STYLE_LIST, "FACTION CREATION", "Type 0(Drugs)\nType 1(Weapons)", "Choose","Cancel");
    }
    else
    {
        // Either a new row in "faction" table wasn't created or `accounts` wasn't updated
    }
}
Reply
#8

Quote:
Originally Posted by Gammix
Посмотреть сообщение
[..] and you don't need to use a callback for that, because mysql_tquery returns 1 or 0 for success and fail.
A query (referring to INSERT) can fail for multiple reasons (duplicate, column that does not exist, column count does not match value count etc.) but the function will return success either way as long as the connection handle is valid.

As for the grave accent character is not mandatory and reduces the size of the query if not used.
Reply
#9

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
A query (referring to INSERT) can fail for multiple reasons (duplicate, column that does not exist, column count does not match value count etc.) but the function will return success either way as long as the connection handle is valid.

As for the grave accent character is not mandatory and reduces the size of the query if not used.
Yes that's completely right but here we are performing everything ourselves, So if i was to do it, i would just do the things right in the query accordingly so i know there can't be any problem.

In this case, he handles factions with a custom ID, not relying on MySQL database (primary keys). So i don't think so there is going to be any problem as soon as he handles it right.
Reply
#10

@Gammix.
It updates my faction name, however it fucked with my factionid. So faction ID are A_I. I'm trying to give my faction name a specific ID and update it both on `account` and `faction` table. under account the colum is called `factionid` and similarly under `faction` it's called `factionid`.

Player[playerid][FactionID] = cache_insert_id(); ->Faction ID.
PHP код:
case factionnamed:
        {
            new 
bool:success true;
            
format(Player[playerid][FactionName], 120inputtext);
            new 
query[200];
            
mysql_format(mysqlquerysizeof (query), "INSERT INTO `faction` (`factionname`) VALUES ('%e')"inputtext);
            if (
mysql_tquery(mysqlquery) == 0)
                
success false;
            
mysql_format(mysqlquerysizeof (query), "UPDATE `accounts` SET `factionname` = '%e', `factionid` = %d WHERE `ID` =  %d LIMIT 1"Player[playerid][FactionName], Faction[playerid][ID], Player[playerid][ID]);
            if (
mysql_tquery(mysqlquery) == 0)
                
success false;
            if (
success)
            {
                
Faction[playerid][ID] = cache_insert_id();
                
printf("[INFO] New faction created. Database ID: [%d]"Faction[playerid][ID]);
                
ShowPlayerDialog(playeridfactiontypedDIALOG_STYLE_LIST"FACTION CREATION""Type 0(Drugs)\nType 1(Weapons)""Choose","Cancel");
            }
            else
            {
                
// Either a new row in "faction" table wasn't created or `accounts` wasn't updated
            
}
        }
        case 
factiontyped:
        {
            if(
response)
                {
                    switch(
listitem)
                    {
                        
//mysql_format(mysql,query,sizeof(query), "UPDATE `accounts` SET `Admin` = %d WHERE ID = %d LIMIT 1",  level, Player[target][ID]);
                        
case 0:
                        {
                            new 
query[500];
                            new 
str[250];
                            
format(str,sizeof(str), "FACTION CREATED %s"Player[playerid][FactionName]);
                            
SendClientMessage(playerid,COLOR_GREY,str);
                            
SendClientMessage(playeridCOLOR_GREY"[INFO] FACTION CREATED!(Drug Warehouse)");
                            
Player[playerid][FactionID] = Faction[playerid][ID];
                            
mysql_format(mysqlquery,sizeof(query), "UPDATE `faction` SET `factiontype` = '0' WHERE ID = %d LIMIT 1"Player[playerid][FactionID]);
                            
mysql_tquery(mysqlquery"""");
                            
                        }
                        
                        case 
1:
                        {
                            new 
query[500];
                            
SendClientMessage(playeridCOLOR_GREY"[INFO] FACTION CREATED!(Gun Warehouse)");
                            
Player[playerid][FactionID] = Faction[playerid][ID];
                             
mysql_format(mysqlquery,sizeof(query), "UPDATE `faction` SET `factiontype` = '1' WHERE ID = %d LIMIT 1",Player[playerid][FactionID]);
                            
mysql_tquery(mysqlquery"""");
                        }
                    }
                }
        } 
Because I'm trying to update faction type, set 0 or 1, depending on what user choose. and then I use WHERE ID %d which signifies to my faction ID. It doesn't update anymore because there is something funky with my faction ID.
However when I use /createfaction -> it creates the faction with a ID. But when I choose the type, it doesn't update.
In short and simple, I'm trying to assign Player[playerid][FactionID] to the A_I created under `faction` table with the name `factionid` so I can just call Player[playerid][FactionID] which will return me the player faction ID.

Also, What if I'd like to load data from `faction` table. How do I use cache_get_data_int and cache_get_data to retrieve items from fields,rows.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)