MySQL导入通过PhpMyAdmin成功导入转储文件。但是通过PHP导入不会';行不通

MySQL导入通过PhpMyAdmin成功导入转储文件。但是通过PHP导入不会';行不通,php,mysql,phpmyadmin,Php,Mysql,Phpmyadmin,好吧,这几天我一直在胡思乱想,不知道发生了什么事。我可以通过phpmyadmin成功导入mysql转储文件,但每当我尝试使用任何php脚本将其导入数据库时,它都会给我错误信息。目前,我正在使用这个脚本 <?php $mysqlDatabaseName = 'dbnname'; $mysqlUserName = 'username'; $mysqlPassword = 'password'; $mysqlHostName = 'localhost'; $mysqlImportFilenam

好吧,这几天我一直在胡思乱想,不知道发生了什么事。我可以通过phpmyadmin成功导入mysql转储文件,但每当我尝试使用任何php脚本将其导入数据库时,它都会给我错误信息。目前,我正在使用这个脚本

<?php

$mysqlDatabaseName = 'dbnname';
$mysqlUserName = 'username';
$mysqlPassword = 'password';
$mysqlHostName = 'localhost';
$mysqlImportFilename = dirname(__FILE__) . '/database.sql';
//DONT EDIT BELOW THIS LINE
//Export the database and output the status to the page
$con = mysqli_connect($mysqlHostName, $mysqlUserName, $mysqlPassword, $mysqlDatabaseName);
if (!$con) {
    die('connection failed');
}
$command = 'mysql -h' . $mysqlHostName . ' -u' . $mysqlUserName . ' -p' . $mysqlPassword . ' ' . $mysqlDatabaseName . ' < ' . $mysqlImportFilename;
$result = mysqli_query($con, $command);
if ($result) {
    print_r('True $result: ' . $result);
} else {
    print_r('False $result: ' . $result . mysqli_errno($con));
} // TODO need to print mysqli_errno
通过phpmyadmin手动导入时没有错误


谢谢

您提供给mysqli的
$命令是对独立的mysql命令行界面的调用,而不是mysqli函数调用。使用PHP system()或类似函数调用外部mysql CLI

试试这个

<?php

// Name of the file
$filename = 'database.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dbnname';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// 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 = '';
}
}
 echo "Tables imported successfully";
?>


祝你好运

那么,你有什么错误?更新了。感谢您的指点。(1)没有必要将
数据库.sql
中的每个查询都放在一行上。它可以是多行的。(2) 此脚本中的大多数
mysql.*
函数现在都已贬值。您需要用
mysqli.*
函数替换它们。我在替换函数后运行了脚本,它给了我错误。因为这个脚本正面临着我上面提到的问题1。好的,这个脚本在完成了我上面所说的之后工作了。你是说这样的:
$result=exec($command)
<?php

// Name of the file
$filename = 'database.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dbnname';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// 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 = '';
}
}
 echo "Tables imported successfully";
?>