澳门新葡萄京官网注册PHP处理SQL脚本文件导入到MySQL的代码实例

布衣蔬食在构建安装程式,数据备份程序的时候会要用到这般的代码,小编看网络有是有不太多,而且有个别亦不是很好用,不经常候这种代码直接用现有的能够省去数不清日子,那么自个儿就从stackoverflow转了贰个回复,要求的恋人能够参照下。

复制代码 代码如下:

<?php

代码如下:

<?php

// Name of the file
$filename = ‘churc.sql’;
// MySQL host
$mysql_host = ‘localhost’;
// MySQL username
$mysql_username = ‘root’;
// MySQL password
$mysql_password = ”;
// Database name
$mysql_database = ‘dump’;

// Name of the file

// Name of the file
$filename = ‘churc.sql’;
// MySQL host
$mysql_host = ‘localhost’;
// MySQL username
$mysql_username = ‘root’;
// MySQL password
$mysql_password = ”;
// Database name
$mysql_database = ‘dump’;

// 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());

$filename = churc.sql;

// 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;

// MySQL host

// 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”;
?>

$mysql_host = localhost;

// 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”;
?>

// MySQL username

您或许感兴趣的随笔:

  • MySQL从命令行导入SQL脚本时现身普通话乱码的解决情势
  • Excel导入Sqlserver数据库脚本
  • MySQL导入sql脚本错误:2007消除方法
  • C#制造数据库及导入sql脚本的法门
  • t-sql/mssql用命令行导入数据脚本的SQL语句示例
  • 有关sql脚本导入Oracle时再也生成check约束的难题消除
  • 选取SQL脚本导入数据到分裂数据库制止再次的3种方法

$mysql_username = root;

// MySQL password

$mysql_password = ;

// Database name

$mysql_database = dump;

// 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 its 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, its the end of the query

if (substr(trim($line), -1, 1) == 😉

{

// Perform the query

mysql_query($templine) or print(Error performing query . $templine . :
. mysql_error() .

);

// Reset temp variable to empty

$templine = ;

}

}

echo Tables imported successfully;

?

发表评论

电子邮件地址不会被公开。 必填项已用*标注