How to add this sql query?

Discussion in 'Databases' started by jacobkell, Aug 2, 2008.

  1. #1
    Query: [Select]

    SHOW COLUMNS
    FROM {$db_prefix}members
    LIKE '$column'
    Query: [Select]

    SHOW COLUMNS
    FROM {$db_prefix}log_activity
    LIKE '$column' Query: [Select]

    ALTER TABLE {$db_prefix}log_activity
    ADD COLUMN $column $spec
    This is what i need to finish install refferal mod for smf board.
    There is also install.php but it say it could not working which showed to be true
    Here is content of that install.php-
    <?php
    /*************************************************
    REFERRALS MOD v2.0
    **************************************************
    INSTALL.PHP
    **************************************************/

    if (!defined('SMF'))
    die('Hacking attempt...');

    // Adds the new columns to the members table - first checks if the column exists, if it doesnt, it adds it
    $newcolumns = array();
    $newcolumns['referrals_no'] = array('referralsno', "mediumint(8) NOT NULL default '0'");
    $newcolumns['referrals_hits'] = array('referral****s', "mediumint(10) NOT NULL default '0'");
    $newcolumns['referred_by'] = array('referredby', "mediumint(8) NOT NULL default '0'");
    $newcolumns['referred_on'] = array('referredon', "int(10) NOT NULL default '0'");
    foreach($newcolumns as $column => $spec)
    {
    // Old type, so rename
    $result = db_query("
    SHOW COLUMNS
    FROM {$db_prefix}members
    LIKE '".$spec[0]."'
    ", __FILE__, __LINE__);

    if (mysql_num_rows($result) != 0)
    {
    db_query("
    ALTER TABLE {$db_prefix}members
    CHANGE ".$spec[0]." $column ".$spec[1]."
    ", __FILE__, __LINE__);
    }
    else
    {
    // Check if already have the new type
    $result = db_query("
    SHOW COLUMNS
    FROM {$db_prefix}members
    LIKE '$column'
    ", __FILE__, __LINE__);

    // No previous install so create it
    if (mysql_num_rows($result) == 0)
    {
    db_query("
    ALTER TABLE {$db_prefix}members
    ADD COLUMN $column ".$spec[1]."
    ", __FILE__, __LINE__);
    }
    }

    // Tidy Up
    unset($column, $spec);
    mysql_free_result($result);
    }
    // Tidy Up
    unset($newcolumns);

    // Adds the new columns to the log activity table - first checks if the column exists, if it doesnt, it adds it
    $newcolumns = array();
    $newcolumns['referrals'] = "smallint(5) NOT NULL default '0'";
    foreach($newcolumns as $column => $spec)
    {
    $result = db_query("
    SHOW COLUMNS
    FROM {$db_prefix}log_activity
    LIKE '$column'", __FILE__, __LINE__);

    if (mysql_num_rows($result) == 0) {
    db_query("
    ALTER TABLE {$db_prefix}log_activity
    ADD COLUMN $column $spec
    ", __FILE__, __LINE__);
    }
    // Tidy Up
    unset($column, $spec);
    mysql_free_result($result);
    }
    // Tidy Up
    unset($newcolumns);
    ?>
    So how do i add that?Could i use phpmyadmin to add it?
     
    jacobkell, Aug 2, 2008 IP
  2. david_t

    david_t Member

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #2
    So, your problem is that you need to create(or more like update) the table?

    $newcolumns['referred_by'] = array('referredby', "mediumint(8) NOT NULL default '0'");
    $newcolumns['referred_on'] = array('referredon', "int(10) NOT NULL default '0'");

    I believe the SQL would look like something like this:
    ALTER TABLE table_name
     ADD(referrals_no mediumint(8) NOT NULL default '0',
     referrals_hits mediumint(10) NOT NULL default '0',
     referred_by mediumint(8) NOT NULL default '0',
     referred_on int(10) NOT NULL default '0');
    Code (markup):
    You will need to change table_name into whatever your *members' table is called.
     
    david_t, Aug 2, 2008 IP