我正在两个平台上工作。一个是装有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中,它仅以某种方式执行.sql文件中的CREATE TABLE tablename
语句,但不执行INSERT INTO表名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","","","","","","","","");
答案 0 :(得分:0)
事实证明,在导入sql文件之前,我必须调整用于导出sql文件的代码。显然,感谢记录他的工作的编码人员,导出脚本实际上是从here复制的。
Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName, $tables=array("table1","table2","table3"), $backup_name="somesql.sql" );
function Export_Database($host,$user,$pass,$name, $tables=false, $backup_name=false )
{
$mysqli = new mysqli($host,$user,$pass,$name);
$mysqli->select_db($name);
$mysqli->query("SET NAMES 'utf8'");
$queryTables = $mysqli->query('SHOW TABLES');
while($row = $queryTables->fetch_row())
{
$target_tables[] = $row[0];
}
if($tables !== false)
{
$target_tables = array_intersect( $target_tables, $tables);
}
foreach($target_tables as $table)
{
$result = $mysqli->query('SELECT * FROM '.$table);
$fields_amount = $result->field_count;
$rows_num = $mysqli->affected_rows;
$res = $mysqli->query('SHOW CREATE TABLE '.$table);
$TableMLine = $res->fetch_row();
$content = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n";
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0)
{
while($row = $result->fetch_row())
{ //when started (and every after 100 command cycle):
if ($st_counter%100 == 0 || $st_counter == 0 )
{
$content .= "\nINSERT INTO ".$table." VALUES";
}
$content .= "\n(";
for($j=0; $j<$fields_amount; $j++)
{
$row[$j] = str_replace("\n","\\n", addslashes($row[$j]) );
if (isset($row[$j]))
{
if($row[$j] === ''){ $content .= 'NULL'; }
else{
if(is_numeric($row[$j])){ $content .= $row[$j]; }
else{ $content .= '"'.$row[$j].'"'; }
}
}
else
{
$content .= '""';
}
if ($j<($fields_amount-1))
{
$content.= ',';
}
}
$content .=")";
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num)
{
$content .= ";";
}
else
{
$content .= ",";
}
$st_counter=$st_counter+1;
}
} $content .="\n\n\n";
}
//$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
//$backup_name = $backup_name ? $backup_name : $name."-".time().".sql";
$backup_name = $backup_name ? $backup_name : "somesql.sql";
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$backup_name."\"");
echo $content; exit;
}
这样生成的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",NULL,NULL,NULL,NULL,NULL,"Some details",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL),
(3,"MK",NULL,NULL,NULL,NULL,NULL,"Some other details",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL),
(4,"CH",NULL,NULL,NULL,NULL,NULL,"Some other other details",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
关键部分是上述函数中的这一行:
if($row[$j] === ''){ $content .= 'NULL'; }
else{
if(is_numeric($row[$j])){ $content .= $row[$j]; }
else{ $content .= '"'.$row[$j].'"'; }
}
现在我可以在两个系统中导入sql文件而不会出错。