adding column to all tables in a mysql database (unknown table names)

前端 未结 4 826
予麋鹿
予麋鹿 2021-02-08 08:49

I need to add a primary key to a set of tables in a given database. I do not know how many tables there will be or what their specific names are. They will all be of the for dat

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-08 09:20

       getAll($sql, $params);
                    $this->_check_error($rows);
                    if(count($rows)>0){
                        $this->_row_count=count($rows);
                        }
    
                    else
                        $this->_row_count=0;
                    return $rows;
                  } 
    
                  //====== fetch  rows  from db and return array of row object
                  function getAll($sqlQuery='',$params= array())
                  {     
                     $msg="";
                      $this->_sqlQuery="";
                      $sQuery=$this->MakeSQLQuery($sqlQuery,$params);
                      $this->_sqlQuery=$sQuery;
                      $res=mysql_query($sQuery) or die(mysql_error());
                      $results = array();
                      if(mysql_num_rows($res)>0){
                      while ($row = mysql_fetch_object($res)) {
                              $results[] = $row;
                      }
                      }
                      return $results;
    
                  }  
    
                   function MakeSQLQuery($str='',$params= array())
                  {
                      foreach($params as $k=>$v){
                       $params[$k]=$this->_quote($params[$k]);
                      }
                      $str=$this->replace_different("?",$params,$str);
                      return $str;
                  }
    
                  function replace_different($search,$replace,$string) {
                   $occs = substr_count($string,$search);
                   $last = 0;
                   $cur = 0;
                   $data = '';
                   for ($i=0;$i<$occs;$i++) {
                     $find = strpos($string,$search,$last);
                     $data .= substr($string,$last,$find-$last).$replace[$cur];
                     $last = $find+strlen($search);
                     if (++$cur == count($replace)) {
                       $cur = 0;
                     }
                   }
                   return $data.substr($string,$last);
                 }
    
                 // check for a database error
                  function _check_error($obj, $msg = 'Database Error')
                  {
    
                    if (!$this->isError($obj)) 
                      {
                        $this->raise_error($msg . ': ' .$obj->getMessage(). mysql_error());
                      }
                      return ;
                  }
    
                  function isError($value)
                    {
                        if(isset($value))
                            return true;
                        else
                            return false;
                    }
    
                }
                // main operation from here 
                //====define object of class========
                $tbl_col = new Model;
                $sqlquery_col="SELECT table_name FROM information_schema.tables where table_schema='".$sLocalDatabaseName."'";
                $res_tab_col=$tbl_col->find_query_all($sqlquery_col);
                foreach($res_tab_col as $r=>$v){
                    $res=mysql_query("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '".$sLocalDatabaseName."' AND TABLE_NAME ='".$v->table_name."' and COLUMN_NAME!='".$Add_columnname."' ORDER BY ORDINAL_POSITION DESC LIMIT 1");
                  if(mysql_num_rows($res)>0){
                     while ($row = mysql_fetch_object($res)) {
                        $last_column= $row->COLUMN_NAME;
                        $alter_query="ALTER TABLE `".$v->table_name."`  ADD `".$Add_columnname."` INT NOT NULL DEFAULT '1' AFTER `".$last_column."` ";
                        mysql_query($alter_query);
                    }
                  }
                }
        ?>
    

提交回复
热议问题