-2

我在两个平台上工作。一台是装有 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","","","","","","","","");
4

1 回答 1

0

事实证明,在导入 sql 文件之前,我必须调整用于导出 sql 文件的代码。显然,感谢记录他的工作的编码器,导出脚本实际上是从这里复制的。

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 文件而不会出错。

于 2016-04-12T01:58:54.490 回答