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');
$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";
$select $this->dbh->getAll('SELECT * FROM '.$table[0]);
$select as $key2=>$row){
$return .= 'INSERT INTO '.$table[0].' VALUES(';
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
$row[$j])) { 
$return .= '"'.$row[$j].'"' 
                        } else { 
$return .= '""'
$j<($num_fields-1)) { 
$return .= ','
$return .= ");\n";