Posted By

neogeek on 07/01/06


Tagged

mysql backup


Versions (?)

Who likes this?

156 people have marked this snippet as a favorite

Bunker
meth
cochambre
jonbaer
Fixe
hides
panatlantica
shamrog12
nicolaspar
groenewege
yuconner
hxseven
ttscoff
clifton
blakeb
peterF
banjomamo
Phoenix
togi
vali29
glex
coggla
JimiJay
DFCNT
Morgano
darthomir
blairski
wbowers
Nils
Baris
hellion
sergiomco
Bron
ibomb
Arzakon
xuanyan
Prankster
mrjthethird
digiloper
blackabee
JustinCrossman
jdstraughan
vince2doom
pablodgavilan
jamesming
ki4ngel
jsalo
nickdoherty
wizard04
sumandahal
hans
Leech
jero
djyrb
canberkol
joaosalless
iconsis
luggnagger
dviero
lostofctrl
baqc
yuindustries
brucealdridge
sarfraznawaz2005
xtheonex
owais
rezzz
irishsk
hamiltonmascioli
mtelligent
ikimozu
mattische
artoodetoo
cjwilburn
nb109
cmahoney
cmellmann
hasantayyar
thadwheeler
redbaron_ict
TheDeveloper
superuntitled
svnlto
umang_nine
Gr33d
mattrichardson
rene-design
sultano
lexpresso
Zwendel
kellyrmartin
ren
benoit
Michael01
alexteg
gosmoon
tea4ge
jrobinsonc
oktijum
leecsargent
philsown
jschilling
NyX
kaartz
sethetter
alessio2
sonnh
dsoms
htl
osirisinternet
sitebuildr
jfherring
iamadams
FreaKzero
g33kish
pytheas
tre
nextneed
pchengsf
miceno
koteus
plasticbottle
fauverism
mogwi
DaveMellett
seanpowell
bobbym245
ijasnijas
bindaskhan2004hotmailcom
hifi
martins
infinidad
pablo808
barbietunnie
ericmuyser
nikefido
tspitzr
verostudios
mfigge
intothelight
technopoetic
qubestream
lfcortes
virendrakryadav
summer_charlie
prit
breign
sjacunningham
peckham
wirenaught
sbcjr
pelted
guilhermehn
shalomfriss
White
clac


MySQL Dump


 / Published in: PHP
 

Creates a backup of a MySQL database in SQL format.

  1. if (!function_exists('mysql_dump')) {
  2.  
  3. function mysql_dump($database) {
  4.  
  5. $query = '';
  6.  
  7. $tables = @mysql_list_tables($database);
  8. while ($row = @mysql_fetch_row($tables)) { $table_list[] = $row[0]; }
  9.  
  10. for ($i = 0; $i < @count($table_list); $i++) {
  11.  
  12. $results = mysql_query('DESCRIBE ' . $database . '.' . $table_list[$i]);
  13.  
  14. $query .= 'DROP TABLE IF EXISTS `' . $database . '.' . $table_list[$i] . '`;' . lnbr;
  15. $query .= lnbr . 'CREATE TABLE `' . $database . '.' . $table_list[$i] . '` (' . lnbr;
  16.  
  17. $tmp = '';
  18.  
  19. while ($row = @mysql_fetch_assoc($results)) {
  20.  
  21. $query .= '`' . $row['Field'] . '` ' . $row['Type'];
  22.  
  23. if ($row['Null'] != 'YES') { $query .= ' NOT NULL'; }
  24. if ($row['Default'] != '') { $query .= ' DEFAULT \'' . $row['Default'] . '\''; }
  25. if ($row['Extra']) { $query .= ' ' . strtoupper($row['Extra']); }
  26. if ($row['Key'] == 'PRI') { $tmp = 'primary key(' . $row['Field'] . ')'; }
  27.  
  28. $query .= ','. lnbr;
  29.  
  30. }
  31.  
  32. $query .= $tmp . lnbr . ');' . str_repeat(lnbr, 2);
  33.  
  34. $results = mysql_query('SELECT * FROM ' . $database . '.' . $table_list[$i]);
  35.  
  36. while ($row = @mysql_fetch_assoc($results)) {
  37.  
  38. $query .= 'INSERT INTO `' . $database . '.' . $table_list[$i] .'` (';
  39.  
  40. $data = Array();
  41.  
  42. while (list($key, $value) = @each($row)) { $data['keys'][] = $key; $data['values'][] = addslashes($value); }
  43.  
  44. $query .= join($data['keys'], ', ') . ')' . lnbr . 'VALUES (\'' . join($data['values'], '\', \'') . '\');' . lnbr;
  45.  
  46. }
  47.  
  48. $query .= str_repeat(lnbr, 2);
  49.  
  50. }
  51.  
  52. return $query;
  53.  
  54. }
  55.  
  56. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ki4ngel on July 19, 2008

Instead of using a "DESCRIBE ... " you can use "SHOW CREATE TABLE ... " which is easier.

Posted By: brucealdridge on January 29, 2009

and to be a pain mysqllisttables has been deprecated, as per php docs (http://nz2.php.net/mysqllisttables) , instead use "SHOW TABLES FROM $database"

Posted By: FederikoNavarro on September 3, 2010

It works!!! You saved my mysql project. I will recommend you web site to my friends.

Posted By: seotab on November 22, 2017

very helpful script to backup site database, thanks I will implement it to backup my site.

Posted By: chrislynn on March 14, 2018

Mysqldump is a piece of the MySQL social database bundle that enables you to "dump" a database, or an accumulation of databases, for reinforcement or transferral to another SQL server. Coursework HelpĀ Service The server that imports the databases do not need to be MySQL.

Posted By: Jacky888 on April 19, 2018

How to root zte zmax pro z981 with or without PC http://bestandroidtoroot.com/article/how-to-root-zte-zmax-pro-z981-with-or-without-pc

Posted By: Gincon on May 10, 2018

Thanks! I'm tired of losing my data, it looks like that's all what I do for the last couple of weeks lol Been rewriting these little reviews of No Need to Study for the third time already. Can anything be done to remind me do backups regularly?

Posted By: Gincon on May 10, 2018

Thanks! I'm tired of losing my data, it looks like that's all what I do for the last couple of weeks lol Been rewriting these little reviews of No Need to Study for the third time already. Can anything be done to remind me do backups regularly?

Posted By: hardwork23 on July 10, 2018

If you need to fix internet certificate errors so you may easily switch to this portal internet explorer certificate errors fix and can have one of the best solution to your problems and will also provide you the exact ways for same purpose. Try out for once.

You need to login to post a comment.