Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Optymalizacja kodu
Forum PHP.pl > Forum > Przedszkole
NetJaro
Mam proźbę - możecie mi pomóc zoptymalizować kod? Aby szybciej backup wykonywało i ogólnie przyśpieszyć.

  1. <?php 
  2.  
  3. /*
  4. Skrypt został napisany przez NetJaro.
  5. Komercyjne rozpowszechnianie skryptu jest ZABRONIONE!
  6. Skrypt jest DARMOWY! Można go dowolnie modyfikować. 
  7.  
  8. Instalacja:
  9. CHMOD 777 na folder w którym znajduje się skrypt.
  10.  
  11. Wymagania:
  12. Jeżeli chcesz, aby skrypt wysyłał na maila backup, serwer MUSI obsługiwać funkcj
  13.  mail();
  14. */
  15. $db_name = 'upload'; //wpisz tutaj bazę której chcesz zrobić backup
  16. $link = mysql_connect('localhost', 'root', 'krasnal') or die('Connection error : ' . mysql_error()); 
  17.  
  18. mysql_select_db($db_name) or die('Could not select database'); 
  19.  
  20. $sql = ''; 
  21. $crlf = &#092;"rn\"; 
  22.  
  23. $time = date('Y m d, H:i', time()); 
  24. $sql .= '#' . $crlf; 
  25. $sql .= '# Zrzut bazy danych ' . $crlf; 
  26. $sql .= '# Wygenerowano: ' . $time . $crlf; 
  27. $sql .= '#' . $crlf . $crlf; 
  28.  
  29. $res = mysql_list_tables($db_name); 
  30. while($row = mysql_fetch_array($res)) { 
  31. $table_name = $row[0]; 
  32. $table_fields = array(); 
  33.  
  34. $sql .= '#' . $crlf; 
  35. $sql .= '# Struktura tabeli ' . $table_name . $crlf; 
  36. $sql .= '#' . $crlf; 
  37.  
  38. $sql .= 'DROP TABLE IF EXISTS ' . $table_name . ';' . $crlf; 
  39. $sql .= 'CREATE TABLE ' . $table_name . ' ( ' . $crlf; 
  40. $res2 = mysql_query('SHOW FIELDS FROM ' . $table_name); 
  41. while($fields = mysql_fetch_array($res2)) { 
  42. $sql .= '  '; 
  43. $sql .= '`'.$fields['Field'].'`' . ' ' . $fields['Type']; 
  44. if (!empty($fields['Default'])) { 
  45. $sql .= ' DEFAULT '' . $fields['Default'] . '''; 
  46. } 
  47.  
  48. if ($fields['Null'] != 'Yes') { 
  49. $sql .= ' NOT NULL'; 
  50. } 
  51.  
  52. if (!empty($fields['Extra'])) { 
  53. $sql .= ' ' . $fields['Extra']; 
  54. } 
  55.  
  56. $sql .= ',' . $crlf; 
  57.  
  58. $table_fields[] = $fields['Field']; 
  59. } 
  60.  
  61. $index = ''; 
  62. $res2 = mysql_query('SHOW KEYS FROM ' . $table_name); 
  63. while ($keys = mysql_fetch_assoc($res2)) { 
  64. $kname = $keys['Key_name']; 
  65. if(($kname != 'PRIMARY') && ($keys['Non_unique'] == 0)) { 
  66. $kname = 'UNIQUE|' . $kname; 
  67. } 
  68.  
  69. $index[$kname] = array(); 
  70. $index[$kname][] = $keys['Column_name']; 
  71. } 
  72.  
  73. while(list($n, $columns) = @each($index)) { 
  74. if ($n == 'PRIMARY') { 
  75. $sql .= '  PRIMARY KEY (`' . implode($columns, '`, `') . '`),'; 
  76. } 
  77. elseif (substr($n, 0, 6) == 'UNIQUE') { 
  78. $sql .= '  UNIQUE `' . substr($n, 7) . '` (`' . implode($columns, '`, `') . '`),'; 
  79. } 
  80. else { 
  81. $sql .= '  KEY `' . $n . '` (`' . implode($columns, '`, `') . '`),'; 
  82. } 
  83. $sql .= $crlf; 
  84. } 
  85.  
  86. $sql .= ');' . $crlf; 
  87. $sql .= $crlf . $crlf; 
  88.  
  89. $sql .= '#' . $crlf; 
  90. $sql .= '# Dane z tabeli ' . $table_name . $crlf; 
  91. $sql .= '#' . $crlf; 
  92. $d_res = mysql_query('SELECT * FROM ' . $table_name); 
  93. while ($data = mysql_fetch_array($d_res)) { 
  94. $sql .= 'INSERT INTO `' . $table_name . '` (`' . implode('`, `', $table_fields) . '`) VALUES('; 
  95.  
  96. $field_count = count($table_fields); 
  97. $f_data = array(); 
  98. for ($i = 0; $i < $field_count; $i++) { 
  99. $data[$i] = addslashes($data[$i]); 
  100. $f_data[] .= ''' . $data[$i] . '''; 
  101. } 
  102. $sql .= implode(', ', $f_data); 
  103. $sql .= ');' . $crlf; 
  104. } 
  105. $sql .= $crlf . $crlf; 
  106. } 
  107. $data = date('Y m d'); 
  108. $sql = str_replace(','.$crlf.');', $crlf.');', $sql); 
  109. $file = fopen(&#092;"$data.sql\", \"w\"); 
  110. fwrite($file, $sql); 
  111. fclose($file); 
  112. // wysyłanie na maila
  113. $myMail = &#092;"TwojMail@costam.pl\"; // Zmien na swój adres E-mail
  114. $myName = &#092;"Imie Nazwisko\"; // Zmien na swoje imie i nazwisko
  115. $temat = &#092;"temat\";
  116.  
  117.  $boundary = &#092;"-->===_54654747_===<---->>4255==_\"; 
  118.  $head = &#092;"From: $myName <$myMail>n\"; 
  119.  $head = $head . &#092;"Reply-To: $myMailn\"; 
  120.  $head = $head . &#092;"X-Mailer: phpn\"; 
  121.  $head = $head . &#092;"X-Sender: <$myMail>n\"; 
  122.  $head = $head . &#092;"Return-Path: <$myMail>n\"; // adres zwrotny dla błędów 
  123.  $head = $head . &#092;"MIME-version: 1.0n\"; 
  124.  $head = $head . &#092;"Content-type: multipart/mixed; \"; 
  125.  $head = $head . &#092;"boundary=\"$boundary\"n\"; 
  126.  $head = $head . &#092;"Content-transfer-encoding: 7BITn\"; 
  127.  $head = $head . &#092;"X-attachments: $data.sql;nn\"; 
  128.  
  129.  $mesg = &#092;"--\" . $boundary . \"n\"; //pamiętamy dwa minusy na początku 
  130.  $mesg = $mesg . &#092;"Content-Type: text/plain; charset=\"us-ascii\"nn\"; 
  131.  $mesg = $mesg . $message . &#092;"n\"; 
  132.  $mesg = $mesg . &#092;"--\" . $boundary . \"n\"; //pamiętamy dwa minusy na początku 
  133.  $mesg = $mesg . &#092;"Content-type: \" . $filename_type . \"; name=\"$data.sql\";n\"; 
  134.  $mesg = $mesg . &#092;"Content-Transfer-Encoding: base64n\"; 
  135.  $mesg = $mesg . &#092;"Content-disposition: attachment; filename= \"$data.sql\"nn\"; 
  136.  $mesg = $mesg . &#092;"--\" . $boundary . \"-- n\"; //pamiętamy dwa minusy na początku i na końcu 
  137.  mail(&#092;"$myMail\",$temat,$mesg,$head);
  138. ?>
Wave
Cytat
$head = $head .

Wystarczy
  1. <?php
  2. $head .= 
  3. ?>
To samo z $mesg.

Cytat
$temat = "temat";

Lepiej by było
  1. <?php
  2. $temat = 'temat';
  3. ?>
ponieważ parser nie musi przeszukiwać stringa pod kątem zmiennych. W ogóle staraj się wszędzie używać ' .

Więcej uwag nie mam bo dokładniej się nie przyglądałem. :]
NetJaro
  1. <?php 
  2.  
  3. /*
  4. Skrypt został napisany przez NetJaro.
  5. Komercyjne rozpowszechnianie skryptu jest ZABRONIONE!
  6. Skrypt jest DARMOWY! Można go dowolnie modyfikować. 
  7.  
  8. Instalacja:
  9. CHMOD 777 na folder w którym znajduje się skrypt.
  10.  
  11. Wymagania:
  12. Jeżeli chcesz, aby skrypt wysyłał na maila backup, serwer MUSI obsługiwać funkcj
  13.  mail();
  14. */
  15. $db_name = 'upload'; //wpisz tutaj bazę której chcesz zrobić backup
  16. $link = mysql_connect('localhost', 'root', 'krasnal') or die('Connection error : ' . mysql_error()); 
  17.  
  18. mysql_select_db($db_name) or die('Could not select database'); 
  19.  
  20. $sql = ''; 
  21. $crlf = &#092;"rn\"; 
  22.  
  23. $time = date('Y m d, H:i', time()); 
  24. $sql .= '#' . $crlf; 
  25. $sql .= '# Zrzut bazy danych ' . $crlf; 
  26. $sql .= '# Wygenerowano: ' . $time . $crlf; 
  27. $sql .= '#' . $crlf . $crlf; 
  28.  
  29. $res = mysql_list_tables($db_name); 
  30. while($row = mysql_fetch_array($res)) { 
  31. $table_name = $row[0]; 
  32. $table_fields = array(); 
  33.  
  34. $sql .= '#' . $crlf; 
  35. $sql .= '# Struktura tabeli ' . $table_name . $crlf; 
  36. $sql .= '#' . $crlf; 
  37.  
  38. $sql .= 'DROP TABLE IF EXISTS ' . $table_name . ';' . $crlf; 
  39. $sql .= 'CREATE TABLE ' . $table_name . ' ( ' . $crlf; 
  40. $res2 = mysql_query('SHOW FIELDS FROM ' . $table_name); 
  41. while($fields = mysql_fetch_array($res2)) { 
  42. $sql .= '  '; 
  43. $sql .= '`'.$fields['Field'].'`' . ' ' . $fields['Type']; 
  44. if (!empty($fields['Default'])) { 
  45. $sql .= ' DEFAULT '' . $fields['Default'] . '''; 
  46. } 
  47.  
  48. if ($fields['Null'] != 'Yes') { 
  49. $sql .= ' NOT NULL'; 
  50. } 
  51.  
  52. if (!empty($fields['Extra'])) { 
  53. $sql .= ' ' . $fields['Extra']; 
  54. } 
  55.  
  56. $sql .= ',' . $crlf; 
  57.  
  58. $table_fields[] = $fields['Field']; 
  59. } 
  60.  
  61. $index = ''; 
  62. $res2 = mysql_query('SHOW KEYS FROM ' . $table_name); 
  63. while ($keys = mysql_fetch_assoc($res2)) { 
  64. $kname = $keys['Key_name']; 
  65. if(($kname != 'PRIMARY') && ($keys['Non_unique'] == 0)) { 
  66. $kname = 'UNIQUE|' . $kname; 
  67. } 
  68.  
  69. $index[$kname] = array(); 
  70. $index[$kname][] = $keys['Column_name']; 
  71. } 
  72.  
  73. while(list($n, $columns) = @each($index)) { 
  74. if ($n == 'PRIMARY') { 
  75. $sql .= '  PRIMARY KEY (`' . implode($columns, '`, `') . '`),'; 
  76. } 
  77. elseif (substr($n, 0, 6) == 'UNIQUE') { 
  78. $sql .= '  UNIQUE `' . substr($n, 7) . '` (`' . implode($columns, '`, `') . '`),'; 
  79. } 
  80. else { 
  81. $sql .= '  KEY `' . $n . '` (`' . implode($columns, '`, `') . '`),'; 
  82. } 
  83. $sql .= $crlf; 
  84. } 
  85.  
  86. $sql .= ');' . $crlf; 
  87. $sql .= $crlf . $crlf; 
  88.  
  89. $sql .= '#' . $crlf; 
  90. $sql .= '# Dane z tabeli ' . $table_name . $crlf; 
  91. $sql .= '#' . $crlf; 
  92. $d_res = mysql_query('SELECT * FROM ' . $table_name); 
  93. while ($data = mysql_fetch_array($d_res)) { 
  94. $sql .= 'INSERT INTO `' . $table_name . '` (`' . implode('`, `', $table_fields) . '`) VALUES('; 
  95.  
  96. $field_count = count($table_fields); 
  97. $f_data = array(); 
  98. for ($i = 0; $i < $field_count; $i++) { 
  99. $data[$i] = addslashes($data[$i]); 
  100. $f_data[] .= ''' . $data[$i] . '''; 
  101. } 
  102. $sql .= implode(', ', $f_data); 
  103. $sql .= ');' . $crlf; 
  104. } 
  105. $sql .= $crlf . $crlf; 
  106. } 
  107. $data = date('Y m d'); 
  108. $sql = str_replace(','.$crlf.');', $crlf.');', $sql); 
  109. $file = fopen(&#092;"$data.sql\", \"w\"); 
  110. fwrite($file, $sql); 
  111. fclose($file); 
  112. // wysyłanie na maila
  113. $myMail = 'TwojMail@costam.pl'; // Zmien na swój adres E-mail
  114. $myName = 'Imie Nazwisko'; // Zmien na swoje imie i nazwisko
  115. $temat = 'temat';
  116.  
  117.  $boundary = &#092;"-->===_54654747_===<---->>4255==_\"; 
  118.  $head = &#092;"From: $myName <$myMail>n\"; 
  119.  $head .= &#092;"Reply-To: $myMailn\"; 
  120.  $head .= &#092;"X-Mailer: phpn\"; 
  121.  $head .= &#092;"X-Sender: <$myMail>n\"; 
  122.  $head .= &#092;"Return-Path: <$myMail>n\"; // adres zwrotny dla błędów 
  123.  $head .= &#092;"MIME-version: 1.0n\"; 
  124.  $head .= &#092;"Content-type: multipart/mixed; \"; 
  125.  $head .= &#092;"boundary=\"$boundary\"n\"; 
  126.  $head .= &#092;"Content-transfer-encoding: 7BITn\"; 
  127.  $head .= &#092;"X-attachments: $data.sql;nn\"; 
  128.  
  129.  $mesg .= &#092;"--\" . $boundary . \"n\"; //pamiętamy dwa minusy na początku 
  130.  $mesg .= &#092;"Content-Type: text/plain; charset=\"us-ascii\"nn\"; 
  131.  $mesg .= $message . &#092;"n\"; 
  132.  $mesg .= &#092;"--\" . $boundary . \"n\"; //pamiętamy dwa minusy na początku 
  133.  $mesg .= &#092;"Content-type: \" . $filename_type . \"; name=\"$data.sql\";n\"; 
  134.  $mesg .= &#092;"Content-Transfer-Encoding: base64n\"; 
  135.  $mesg .= &#092;"Content-disposition: attachment; filename= \"$data.sql\"nn\"; 
  136.  $mesg .= &#092;"--\" . $boundary . \"-- n\"; //pamiętamy dwa minusy na początku i na końcu 
  137.  mail(&#092;"$myMail\",$temat,$mesg,$head);
  138. ?>


Coś może jeszcze? Tak wygląda aktualny skrypt.
Ociu
wywal:
  1. <?php
  2.  $sql .= '#' . $crlf;
  3. $sql .= '# Struktura tabeli ' . $table_name . $crlf;
  4. $sql .= '#' . $crlf;
  5.  
  6. ?>
Bo wcale Ci nie jest potrzebne
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.