我在两个平台上工作。一台是装有 XAMPP 的 Windows 10 计算机,另一台是装有默认 Apache、PHP 和 MySQL 的 Mac OS X El Capitan。目标是将从远程服务器下载的 .sql 文件导入本地服务器。代码如下:
$dbhost = 'localhost';
$dbuser = 'someuser';
$dbpass = 'somepassword';
$dbname = 'somedb';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$filename = "somesql.sql";
$sqlSource = file_get_contents($filename);
$cmp = $mysqli->multi_query($sqlSource);
if($cmp){ $message = 'Import Successful'; }
else{ $message = 'Import Unsuccessful | '.$mysqli->error; }
但是,上面的代码在 Windows 10 计算机上的 XAMPP 中有效,但在 Mac OS X 上无效。在 Mac OS X 中,它以某种方式仅执行tablename
.sql 文件中的 CREATE TABLE 语句,而不是 INSERT INTO tablename VALUES 位。它不会产生任何错误。
它也不适用于单独安装了 Apache、PHP 和 MySQL 的 Windows。
是否有我遗漏的东西或者我需要调整配置以使其在 Mac OS X 上运行?
编辑:下面是生成的 sql 文件 somesql.sql:
CREATE TABLE `sometable` (
`id` smallint(9) unsigned NOT NULL AUTO_INCREMENT,
`var_1` varchar(200) DEFAULT NULL,
`var_2` varchar(200) DEFAULT NULL,
`var_3` varchar(200) DEFAULT NULL,
`int_1` int(100) DEFAULT NULL,
`int_2` int(100) DEFAULT NULL,
`int_3` int(100) DEFAULT NULL,
`text_1` text COMMENT 'depot address',
`text_2` text,
`text_3` text,
`decimal_1` decimal(10,2) DEFAULT NULL,
`decimal_2` decimal(10,2) DEFAULT NULL,
`decimal_3` decimal(10,2) DEFAULT NULL,
`datetime_1` datetime DEFAULT NULL,
`datetime_2` datetime DEFAULT NULL,
`datetime_3` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `var_1` (`var_1`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;
INSERT INTO sometable VALUES
("2","BN","","","","","","Some details","","","","","","","",""),
("3","MK","","","","","","Some other details","","","","","","","",""),
("4","CH","","","","","","Some other other details","","","","","","","","");