我正在尝试使用http://www.a2zwebhelp.com/php-script-to-import-mysql-database中的以下代码恢复数据库 但我收到"错误:查询是空的" 我哪里错了?
P.S。从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 = '';
}
}
?>
答案 0 :(得分:0)
嗯,首先,这部分代码不会跳过评论,它会将它们添加到您的$templine
:
if(substr($line, 0, 2) == '--' || $line == '') //Skip all comments
$templine.=$line;
其次,在这里,您尝试使用上面指定的$templine
执行查询(如果已分配,或以其他方式''
),您实际上想要使用$line
执行查询:< / p>
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>');
}
}
答案 1 :(得分:0)
略有改进并且有效:) @ 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 = '';
}
}
?>