Восстановление базы данных mysql дает ошибки

Я пытаюсь восстановить базу данных, используя следующий код из http://www.a2zwebhelp.com/php-script-to-import-mysql-database Но я получаю сообщение «Ошибка: запрос был пуст». Где я ошибаюсь?

Ps, импортирующий файл sql из phpmyadmin, служит цели, но не работает с этим.

<?php include 'connect.php'; $filename = 'DB_Backups/db-backup-08-04-14-08-39-16.sql'; $templine = ''; $lines = file($filename); //Read entire file foreach($lines as $line){ if(substr($line, 0, 2) == '--' || $line == '') //Skip all comments $templine.=$line; if(substr(trim($line), -1, 1) == ';'){ mysql_query($templine) or print('Error: '.mysql_error().'<br>'); $templine = ''; } } ?> 

Ну, во- $templine , эта часть кода не пропускает комментарии, она буквально добавляет их в ваш $templine :

  if(substr($line, 0, 2) == '--' || $line == '') //Skip all comments $templine.=$line; 

Во-вторых, здесь вы пытаетесь выполнить запрос с $templine назначенным выше (если он когда-либо был назначен, или иначе '' ), где вы действительно хотите выполнить запрос с помощью $line :

  if(substr(trim($line), -1, 1) == ';'){ mysql_query($templine) or print('Error: '.mysql_error().'<br>'); 

Итак, в основном это должно работать несколько лучше:

 foreach($lines as $line){ if(substr($line, 0, 2) == '--' || $line == '') //Skip all comments continue; if(substr(trim($line), -1, 1) == ';'){ mysql_query(trim($line)) or print('Error: '.mysql_error().'in ' . $line . '<br>'); } } 

Небольшое улучшение, и он работает 🙂 @ favoretti благодарим вас за подсказку.

 <?php // Name of the file $filename = 'DB_Backups/'.$name; // Temporary variable, used to store current query $templine = ''; // Read in entire file $lines = file($filename); // Loop through each line foreach ($lines as $line) { // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') continue; // Add this line to the current segment $templine .= $line; // If it has a semicolon at the end, it's the end of the query if (substr(trim($line), -1, 1) == ';') { // Perform the query mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />'); // Reset temp variable to empty $templine = ''; } } ?>