Created
March 24, 2010 07:38
-
-
Save acdimalev/342072 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$link = mysql_connect('localhost', 'root'); | |
function mysql_results($result) { | |
$foos = array(); | |
while ($row = mysql_fetch_row($result)) { | |
$foo = $row[0]; | |
array_push($foos, $foo); | |
} | |
return $foos; | |
} | |
function select_databases($link) { | |
$result = mysql_query('SHOW DATABASES', $link); | |
return mysql_results($result); | |
} | |
function select_tables($link) { | |
$result = mysql_query('SHOW TABLES', $link); | |
return mysql_results($result); | |
} | |
function csvFromResult($stream, $result, $showColumnHeaders = true) { | |
if($showColumnHeaders) { | |
$columnHeaders = array(); | |
$nfields = mysql_num_fields($result); | |
for($i = 0; $i < $nfields; $i++) { | |
$field = mysql_fetch_field($result, $i); | |
$columnHeaders[] = $field->name; | |
} | |
fputcsv($stream, $columnHeaders); | |
} | |
$nrows = 0; | |
while($row = mysql_fetch_row($result)) { | |
fputcsv($stream, $row); | |
$rows++; | |
} | |
return $nrows; | |
} | |
$databases = select_databases($link); | |
foreach ($databases as $database) { | |
mysql_select_db($database); | |
$tables = select_tables($link); | |
mkdir($database); | |
foreach ($tables as $table) { | |
echo "$database.$table\n"; | |
$file = fopen("$database/$table.csv", 'w'); | |
$result = mysql_query("SELECT * FROM $table", $link); | |
csvFromResult($file, $result); | |
fclose($file); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment