I have the following script to export MySQL tables. The SHOW CREATE and DROP IF EXISTS show fine. However, when I loop through the table data, it creates multiple instances of the same query. I think there is something wrong with the for loop. Any ideas? Here's the code;

PHP Code:
$tables = array();
        
$result $this->dbh->getAll('SHOW TABLES');
        foreach(
$result as $key=>$table){
            
$fields $this->dbh->query('SELECT * FROM '.$table[0]);
            
$num_fields $fields->numCols();
            
$return .= 'DROP TABLE IF EXISTS '.$table[0].';';
            
$create $this->dbh->getRow('SHOW CREATE TABLE '.$table[0]);
            
$return .= "\n\n".$create[1].";\n\n";
            
            for(
$i=0;$i<$num_fields;$i++){
                
$select $this->dbh->getAll('SELECT * FROM '.$table[0]);
                foreach(
$select as $key2=>$row){
                    
$return .= 'INSERT INTO '.$table[0].' VALUES(';
                    for(
$j=0;$j<$num_fields;$j++){
                        
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
                        if(isset(
$row[$j])) { 
                            
$return .= '"'.$row[$j].'"' 
                        } else { 
                            
$return .= '""'
                        }
                        if(
$j<($num_fields-1)) { 
                            
$return .= ','
                        }
                    }
                    
$return .= ");\n";
                }
            }
            
$return.="\n\n\n";
        }