Skip to content

Instantly share code, notes, and snippets.

@udnisap
Created April 26, 2014 12:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save udnisap/11319465 to your computer and use it in GitHub Desktop.
Save udnisap/11319465 to your computer and use it in GitHub Desktop.
$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;
@flystyle
Copy link

add after line #1:

$con->set_charset("utf8");

and replace line #42 with:

if ($i < $num_rows) { 
    $return .= "),\n"; 
} else { 
    $return .= ");\n\n"; 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment