PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>PHP CLASS> Alter Mysql Table Class

Alter Mysql Table Class

FROM: AUTHOR: TIME:2008-04-20 HITS:
<?php
/**
* @Version: 1.0.0
* @Author: Muhammed Imran Hussain
* @E-mail: xpimrans@yahoo.co.uk, solutiongiant@gmail.com
* @Website: imran.solutiongiant.com
* @Mobile: +8801714110953
* @Date: 11th March 2007
* @Description: This class generate database table auto increment query
* Most often, dumping mysql table from one host to another
* database table auto increment keyword may not work. Using this class this problem may solve
*/

/**
* Alter Table Class
*/

class ClsAlterDb extends ClsDb
{        
    
/** @var string mysql database name */ phpsu
    
var $config_db            = null;
    
    
/** @var string mysql database host name */
    
var $config_host          = null;
    
    
/** @var string mysql database user name */
    
var $config_user          = null; do you kown phpsu.com?
    
    
/** @var string mysql database password */
    
var $config_password      = null;
    
    
    
/**
     * Constructor
     * @param string DBNAME, HOSTNAME, USERNAME, PASSWORD
    */
    
    
function ClsAlterDb($db, $host, $user, $passowrd){     phpsu is a phpschool
        
/** Initilizing class variable */
        
$this->config_db = $db;
        
$this->config_host = $host;
        
$this->config_user = $user;
        
$this->config_password = $passowrd; phpsu
        
        
$this->ClsDb();    
            
        
/** Getting database table name */
        
$tbl_result = $this->getDbTableList();
        
        foreach(
$tbl_result as $k=>$v){            

phpsu


            
$tbl_name = $tbl_result[$k];
            
            
/** Getting database table meta data */
            
$tbl_fields = $this->getTableFields($tbl_name);
            
            foreach(
$tbl_fields as $k=>$v){ http://www.phpsu.com
                if(
$tbl_fields[$k]['Extra'] == 'auto_increment'){                
                    
$field_name = $tbl_fields[$k]['Field'];        
                    
$field_type = $tbl_fields[$k]['Type'];     welcome to phpsu.com
                    
                    
/** Creating alter query */                
                    
$alter_sql .= 'ALTER TABLE `'.$tbl_name.'` CHANGE `'.$field_name.'` `'.$field_name.'` '.$field_type.' NOT NULL AUTO_INCREMENT;'."\n";

www.phpsu.com


                
                }            
            }
        }
        
        
/** Forcing to download script as a file */    
        
$this->forceFileToDownload($alter_sql, $this->config_db); welcome to phpsu.com
    }
    
    
/**
     * Force to download data as a file
     * @param string DATA, FILE_SUFFIX
    */
    
function forceFileToDownload($alter_sql, $db){
        
header("Content-type: application/x-msdownload");
        
header("Content-Disposition: attachment; filename=alter_".$db.".sql");        
www.phpsu.com

        
header("Pragma: no-cache");        
        
header("Expires: 0");        
        print
"".$alter_sql."";
    }
}

/**
* MySQL DB class
*/

class ClsDb
{             phpsu is a phpschool
    
    
/** @var string query */
    
var $query      = null;
    
    
/** @var string query result */
    
var $result      = null;
    
    
/**
     * Constructor
     * Connect database server and select database
     * @param
    */
    
function ClsDb(){        
phpsu.com

        @
mysql_connect($this->config_host, $this->config_user, $this->config_password) or die("could not connect database server");        
        @
mysql_select_db($this->config_db) or die("could not select database"); phpsu.com is a free phpscool
    }
    
    
    
/**
     * return query result
     * @param
    */
    
function setQuery(){
        if(!
$this->result = @mysql_query($this->query)) return false;
        else return
$this->result; do you kown phpsu.com?
    }
    
    
    
/**
     * Ereturn affected rows
     * @param
    */
    
function getRows(){
        return
$rows = @mysql_num_rows($this->result);
    }
    
    
    
/**
     * Set result free
     * @param
    */ www.phpsu.com
    
function getFreeResult() {
        @
mysql_free_result($this->result);
    }
    
    
    
/**
     * return result array
     * @param
    */
    
function getFetchArray(){
        
$array = false;
        
$this->setQuery();

http://www.phpsu.com


        
        while(
$result_array = @mysql_fetch_array($this->result))
            
$array[] = $result_array;
            
        
$this->getFreeResult();
        return
$array;
    } phpsu提供的php教程
    
    
    
/**
     * return database table list result
     * @param
    */
    
function setListTablesQuery(){
        return
$this->result = @mysql_list_tables($this->config_db);
    }
    
    
    
/**
     * return database table name phpsu is a phpschool
     * @param Int number
    */
    
function getTableName($i){
        return @
mysql_tablename($this->result, $i);
    }
    
    
    
/**
     * return database table name array
     * @param
    */
    
function getDbTableList(){ phpsu is a phpschool
        
$array = false;
        
        
$this->setListTablesQuery();
        
$total_tables = $this->getRows();
        
        for (
$i = 0; $i < $total_tables; $i++) { www.phpsu.com
            
$array[] = $this->getTableName($i);
        }
            
        
$this->getFreeResult();
        return
$array;
    }
    
    
    
/**
     * return database table meta

phpsu.com


     * @param string table name
    */
    
function getTableFields($tbl_name){
        
$this->query = "SHOW COLUMNS FROM ".$tbl_name."";        
        return
$this->result = $this->getFetchArray();

phpsu.com


    }
}


/**
* creating alter class object
* @param string DATABASE_NAME, DATABASE_HOSTNAME, DATABASE_USERNAME, DATABASE_PASSWORD
*/
$obj = new ClsAlterDb('DB_NAME', 'HOST', 'USERNAME', 'PASSWORD');
?>

TITLE:Alter Mysql Table Class
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:56:39 GMT-8 2008