繁体   English   中英

恢复MySQL数据库会出现错误

[英]Restoring mysql database gives errors

我正在尝试使用以下代码从http://www.a2zwebhelp.com/php-script-to-import-mysql-database还原数据库,但是我收到“错误:查询为空”我在哪里出错?

ps从phpmyadmin导入sql文件达到了目的,但是不能使用它。

<?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

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

其次,在这里您尝试使用$templine分配的$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>');
    }
}

稍有改进,就可以了:) @avouretti感谢您的提示。

<?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 = '';
    }
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM