[MySQL] Update through a function
#1

So I'm trying to make a function that you can update a specific field you want in a specific table you want with a specific value you want (so many specific )... but the problem is the value could be a string, an integer or a float... the solution I found is to use valstr on an integer/flat before you call the function, for example:
Also I don't know if it works as I never tested it, so I need your thoughts on that too.
PHP код:
new szString[6] = valstr(8);
Update("players""cash"FIELD_TYPE_INTszString"player_id"g_Player[playerid][ID]) 
And then use strval to turn it to an integer back, but it doesn't seem efficient and I know there's a better solution for it with low-level scripting but I don't know how...
PHP код:
UpdateField(const table_name[], const table_field[], const field_type, const field_value[], const condition_field[], const condition_value)
{
    new 
szQuery[128];
    switch(
field_type)
    {
        case 
FIELD_TYPE_STRING:
        {
            
mysql_format(connectionHandleszQuerysizeof(szQuery), "UPDATE `%e` SET `%e` = '%e' WHERE `%e` = '%d'",
                
table_nametable_fieldfield_valuecondition_fieldcondition_value);
        }
        case 
FIELD_TYPE_INTEGER:
        {
            new 
iValue strval(field_value);
            
mysql_format(connectionHandleszQuerysizeof(szQuery), "UPDATE `%e` SET `%e` = '%d' WHERE `%e` = '%d'",
                
table_nametable_fieldiValuecondition_fieldcondition_value);
        }
        case 
FIELD_TYPE_FLOAT:
        {
            new 
iValue floatstr(field_value);
            
mysql_format(connectionHandleszQuerysizeof(szQuery), "UPDATE `%e` SET `%e` = '%f' WHERE `%e` = '%d'",
                
table_nametable_fieldiValuecondition_fieldcondition_value);
        }
    }
    
mysql_tquery(connectionHandleszQuery"""");
    return 
1;

Forgot to mention - I did some research and I found out about the YSI/y_va but I don't know how to implement it (if possible) in my case.
Reply
#2

This is fine if you want to update one thing at a time, but I have a feeling that you're going to list them beneath each other and in that case it's a very bad thing to do. You may want to check the ORM functions 'cause that seems pretty much like what you're trying to achieve.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
This is fine if you want to update one thing at a time, but I have a feeling that you're going to list them beneath each other and in that case it's a very bad thing to do. You may want to check the ORM functions 'cause that seems pretty much like what you're trying to achieve.
I would be using it 'on-change', for example, the admin changes the faction's name so:
PHP код:
UpdateField("factions""faction_name"FIELD_TYPE_STRING"New Faction Name""faction_id"g_Faction[factionid][ID]); 
I also thought about ORM but isn't the method I'm trying to do would be faster? Because when you use orm_save it updates the entire player's fields and not the field(s) that has been changed.
Also if I'll be using ORM, I would also use standard queries (tquery/pquery), so using those together with ORM would be okay?

Thank you for your help!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)