Created
April 26, 2014 12:56
-
-
Save udnisap/11319465 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
$con = mysqli_connect ( $MYSQL_HOSTNAME, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DATABASE ); | |
$tables = array (); | |
$result = mysqli_query ( $con, 'SHOW TABLES' ); | |
while ( $row = mysqli_fetch_array ( $result ) ) { | |
$tables [] = $row [0]; | |
} | |
// cycle through the tables | |
foreach ( $tables as $table ) { | |
$result = mysqli_query ( $con, "SELECT * FROM `$table`"); | |
$num_fields = mysqli_num_fields ( $result ); | |
$num_rows = mysqli_num_rows( $result ); | |
$return .= "--\n-- Structure for the table $table\n--\n\n"; | |
$return .= "DROP TABLE IF EXISTS `$table`;"; | |
$row2 = mysqli_fetch_array ( mysqli_query ( $con, "SHOW CREATE TABLE `$table`" ) ); | |
$return .= "\n\n" . $row2 [1] . ";\n\n"; | |
if ($num_rows > 0) { | |
$return .= "--\n-- Data dump for the table $table\n--\n\n"; | |
} | |
$i = 0; | |
while ( $row = mysqli_fetch_array ( $result ) ) { | |
if ($i == 0) { | |
$return .= "INSERT INTO `$table` VALUES\n"; | |
} | |
$i++; | |
for($j = 0; $j < $num_fields; $j ++) { | |
if ($j == 0) { | |
$return .= '('; | |
} | |
$row [$j] = addslashes ( $row [$j] ); | |
$row [$j] = mysqli_real_escape_string ( $con, $row [$j] ); | |
if (isset ( $row [$j] )) { | |
$return .= '"' . $row [$j] . '"'; | |
} else { | |
$return .= '""'; | |
} | |
if ($j < ($num_fields - 1)) { | |
$return .= ','; | |
} | |
} | |
$return .= "), \n"; | |
} | |
} | |
echo $return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add after line #1:
and replace line #42 with: