Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> backup wybranych tabel z bazy - jak spod php ?
B3T0N
post
Post #1





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 12.10.2003

Ostrzeżenie: (0%)
-----


smile.gif mam taki plik , ktory zrzuca cala baze danych do pliku test.sql

  1. <?php 
  2. $db_name = 'moja baza'; 
  3. $link = mysql_connect('localhost', 'login', 'passs') or die('Connection error : ' . mysql_error()); 
  4.  
  5. mysql_select_db($db_name) or die('Could not select database'); 
  6.  
  7. $sql = ''; 
  8. $crlf = &#092;"rn\"; 
  9.  
  10. $time = date('Y m d, H:i', time()); 
  11. $sql .= '#' . $crlf; 
  12. $sql .= '# Zrzut bazy danych ' . $crlf; 
  13. $sql .= '# Wygenerowano: ' . $time . $crlf; 
  14. $sql .= '#' . $crlf . $crlf; 
  15.  
  16. $res = mysql_list_tables($db_name); 
  17. while($row = mysql_fetch_array($res)) { 
  18. $table_name = $row[0]; 
  19. $table_fields = array(); 
  20.  
  21. $sql .= '#' . $crlf; 
  22. $sql .= '# Struktura tabeli ' . $table_name . $crlf; 
  23. $sql .= '#' . $crlf; 
  24.  
  25. $sql .= 'DROP TABLE IF EXISTS ' . $table_name . ';' . $crlf; 
  26. $sql .= 'CREATE TABLE ' . $table_name . ' ( ' . $crlf; 
  27. $res2 = mysql_query('SHOW FIELDS FROM ' . $table_name); 
  28. while($fields = mysql_fetch_array($res2)) { 
  29. $sql .= '  '; 
  30. $sql .= '`'.$fields['Field'].'`' . ' ' . $fields['Type']; 
  31. if (!empty($fields['Default'])) { 
  32. $sql .= ' DEFAULT '' . $fields['Default'] . '''; 
  33. } 
  34.  
  35. if ($fields['Null'] != 'Yes') { 
  36. $sql .= ' NOT NULL'; 
  37. } 
  38.  
  39. if (!empty($fields['Extra'])) { 
  40. $sql .= ' ' . $fields['Extra']; 
  41. } 
  42.  
  43. $sql .= ',' . $crlf; 
  44.  
  45. $table_fields[] = $fields['Field']; 
  46. } 
  47.  
  48. $index = ''; 
  49. $res2 = mysql_query('SHOW KEYS FROM ' . $table_name); 
  50. while ($keys = mysql_fetch_assoc($res2)) { 
  51. $kname = $keys['Key_name']; 
  52. if(($kname != 'PRIMARY') && ($keys['Non_unique'] == 0)) { 
  53. $kname = 'UNIQUE|' . $kname; 
  54. } 
  55.  
  56. $index[$kname] = array(); 
  57. $index[$kname][] = $keys['Column_name']; 
  58. } 
  59.  
  60. while(list($n, $columns) = @each($index)) { 
  61. if ($n == 'PRIMARY') { 
  62. $sql .= '  PRIMARY KEY (`' . implode($columns, '`, `') . '`),'; 
  63. } 
  64. elseif (substr($n, 0, 6) == 'UNIQUE') { 
  65. $sql .= '  UNIQUE `' . substr($n, 7) . '` (`' . implode($columns, '`, `') . '`),'; 
  66. } 
  67. else { 
  68. $sql .= '  KEY `' . $n . '` (`' . implode($columns, '`, `') . '`),'; 
  69. } 
  70. $sql .= $crlf; 
  71. } 
  72.  
  73. $sql .= ');' . $crlf; 
  74. $sql .= $crlf . $crlf; 
  75.  
  76. $sql .= '#' . $crlf; 
  77. $sql .= '# Dane z tabeli ' . $table_name . $crlf; 
  78. $sql .= '#' . $crlf; 
  79. $d_res = mysql_query('SELECT * FROM ' . $table_name); 
  80. while ($data = mysql_fetch_array($d_res)) { 
  81. $sql .= 'INSERT INTO `' . $table_name . '` (`' . implode('`, `', $table_fields) . '`) VALUES('; 
  82.  
  83. $field_count = count($table_fields); 
  84. $f_data = array(); 
  85. for ($i = 0; $i < $field_count; $i++) { 
  86. $data[$i] = addslashes($data[$i]); 
  87. $f_data[] .= ''' . $data[$i] . '''; 
  88. } 
  89. $sql .= implode(', ', $f_data); 
  90. $sql .= ');' . $crlf; 
  91. } 
  92. $sql .= $crlf . $crlf; 
  93. } 
  94.  
  95. $sql = str_replace(','.$crlf.');', $crlf.');', $sql); 
  96. $file = fopen('test.sql', 'w'); 
  97. fwrite($file, $sql); 
  98. fclose($file); 
  99.  
  100. ?>


hmmm, czy jest prosty sposob na eksport tylko WYBRANYCH tabel z bazy danych w ten sam sposob , jaki robi ten skrypt ?


--------------------
PC-TUNING.PL - ZMODYFIKUJ SWÓJ KOMPUTER !!!
Go to the top of the page
+Quote Post
Spirit86
post
Post #2





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


to zrzuca wszyskie tabele, nie tylko wybrane ...


--------------------
Audio: Metallica, Soil, RHCP, OffSpring, Green Day, "Retro", Gorillaz, Disturbed, Coma
DB: MySQL 4.1 | php: 4.4.3 Pomogłem Ci? Wciśnij przycisk POMÓGŁ.
Go to the top of the page
+Quote Post
B3T0N
post
Post #3





Grupa: Zarejestrowani
Postów: 55
Pomógł: 0
Dołączył: 12.10.2003

Ostrzeżenie: (0%)
-----


Cytat(Spirit86 @ 2005-04-25 09:58:37)
to zrzuca wszyskie tabele, nie tylko wybrane ...

dzieki ze mnie uswiadomiles blink.gif


--------------------
PC-TUNING.PL - ZMODYFIKUJ SWÓJ KOMPUTER !!!
Go to the top of the page
+Quote Post
Spirit86
post
Post #4





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


sorka, nie przeczytałem całego posta, nie przespana noc tongue.gif
zrób sobie tablicę z nazwami tabel, które chcesz eksportować, później pobieraj tylko dane z tablicy


--------------------
Audio: Metallica, Soil, RHCP, OffSpring, Green Day, "Retro", Gorillaz, Disturbed, Coma
DB: MySQL 4.1 | php: 4.4.3 Pomogłem Ci? Wciśnij przycisk POMÓGŁ.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 02:37