Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * DUMP MYSQL TABLE FROM PHP
- * credits:
- * David Walsh - original script http://davidwalsh.name/backup-mysql-database-php
- * Emanuele "ToX" Toscano - single insert into instruction for many rows, dump & create table functions
- */
- function dumpTable($host, $user, $pass, $db, $table) {
- $return = null;
- $link = mysql_connect($host, $user, $pass);
- mysql_select_db($db, $link);
- $result = mysql_query('SELECT * FROM ' . $table);
- $num_fields = mysql_num_fields($result);
- /* DUMP TABLE (uncomment if you need it) *\/
- $return.='DROP TABLE ' . $table . ';';
- /* CREATE TABLE (uncomment if you need it) *\/
- $row2=mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
- $return.="\n\n" . $row2[1] . ";\n\n";
- /* INSERT INTO instructions */
- if ($num_fields > 0) {
- $return.='INSERT INTO ' . $table . ' VALUES';
- for ($i=0; $i<$num_fields; $i++) {
- $x = 0;
- while ($row=mysql_fetch_row($result)) {
- $x++;
- if ($x == 1) {
- $return .= "\n(";
- } else {
- $return .= "),\n(";
- }
- for ($j=0; $j<$num_fields; $j++) {
- $row[$j]=addslashes($row[$j]);
- $row[$j]=str_replace("\n", "\\n", $row[$j]);
- if (isset($row[$j])) {
- $return.='"' . $row[$j] . '"';
- } else {
- $return.='""';
- }
- if ($j<($num_fields-1)) {
- $return.=',';
- }
- }
- }
- }
- $return.=");\n";
- }
- return $return;
- }
Add Comment
Please, Sign In to add comment