Topic: Best Way to Add Fields to User Table in MySQL?

What would be the best way to add this data to the user table?

    $schema = array(
        'FIELDS'            => array(
            'continent'            => array(
                'datatype'        => 'INT(2)',
                'allow_null'    => true
            ),
            'x'                    => array(
                'datatype'        => 'INT(3)',
                'allow_null'    => true,
                'default'        => '0'
            ),
            'y'                    => array(
                'datatype'        => 'INT(3)',
                'allow_null'    => true,
                'default'        => '0'
            ),
            'alliance'            => array(
                'datatype'        => 'VARCHAR(255)',
                'allow_null'    => true
            ),
            'tournament_field'    => array(
                'datatype'        => 'INT(2)',
                'allow_null'    => true
                'default'        => '0'
            ),
            'game_status'        => array(
                'datatype'        => 'INT(1)',
                'allow_null'    => true,
                'default'        => '0',
            ),
            'castles'            => array(
                'datatype'        => 'INT(3)',
                'allow_null'    => false,
                'default'        => '0'
            )
        )
    );

    $forum_db->add_field('users', $schema);

or some variation of it?

Re: Best Way to Add Fields to User Table in MySQL?

First of all you need to modify array a little (added ","):

           'tournament_field'    => array(
                'datatype'        => 'INT(2)',
                'allow_null'    => true,
                'default'        => '0'
            ),

There is no special method to add many fields in DB. But you can add it one by one:

foreach ($schema['FIELDS'] as $field_name => $field_data)
        $forum_db->add_field('users', $field_name, $field_data['datatype'], $field_data['allow_null'], (isset($field_data['default']) ? $field_data['default'] : null));    
    

Re: Best Way to Add Fields to User Table in MySQL?

Thank you for taking the time to answer and finding that syntax error above. I didn't even notice I had it.

I'm learning as I go along and I really appreciate the help. smile These examples are really helping me.

4 (edited by slickplaid 2009-02-10 10:59)

Re: Best Way to Add Fields to User Table in MySQL?

Would this be the best way to uninstall?

$schema = array('continent', 'x', 'y', 'alliance', 'tournament_field', 'game_status', 'castles');
foreach ($schema)
    $forum_db->drop_field('users', $schema);

Or does one of the $schema variables need to be $schema[] to work properly?

I would just try it out but I'm afraid I'd be dropping the wrong fields.

Re: Best Way to Add Fields to User Table in MySQL?

Actually, it'd have to be

$schema = array('continent', 'x', 'y', 'alliance', 'tournament_field', 'game_status', 'castles');
foreach ($schema as $value)
    $forum_db->drop_field('users', $value);

Right?

6 (edited by slickplaid 2009-02-10 11:14)

Re: Best Way to Add Fields to User Table in MySQL?

Also, do I need to be using "unset($varname)" for each of my variables if they're only used once in a specific section?