本站网址: YippeeSoft开心软件
20070518 php import sql txt
http://www.yippeesoft.com
php导入SQL文本文件,发现失败
问题出现在该文本文件加了UTF8头,晕倒~
<?php
$link = mysql_pconnect(" http://www.yippeesoft.com ", "root", " http://www.yippeesoft.com ")
or die("Could not connect: " . mysql_error());
$result = mysql_query("CREATE DATABASE if not exists `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;")
or die("Invalid query: " . mysql_error());
/* if (mysql_create_db("my_db")) &leftsign;
print ("Database created successfully\\n");
&rightsign; else &leftsign;
printf ("Error creating database: %s\\n", mysql_error());
&rightsign;
$lines = file(\’C:\\tt.sql\’);
foreach ($lines as $line_num => $line)
&leftsign;
$query = $line;
print \’<br>\’;
print "$query";
print \’<br>\’;
$result = mysql_query("$query") or die("Query failed");
&rightsign;*/
// get contents of a file into a string
$filename = "c:\\sql.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
$contents = str_replace("\\r\\n","\\n",$contents);
$qs = explode(";\\n", trim($contents));
$i=count($qs);
print $i;
mysql_select_db(\’my_db\’, $link) or die (\’Can\\\’t use foo : \’ . mysql_error());
for ($i=0;$i<count($qs);$i++)
&leftsign;
$sql=$qs[$i];
// print $qs[$i];
//$sql = str_replace("\\n","",$sql);
$sql=$sql.chr(13).chr(10);
print $sql;
$resultt = mysql_query($sql) or die("Invalid query: " . mysql_error());
// $resultt = mysql_query($qs[$i]) or die("Query failed");
&rightsign;
?>
<?php
$file_name="c:\\sql.txt"; //要导入的SQL文件名
$dbhost=" http://www.yippeesoft.com "; //数据库主机名
$dbuser="root"; //数据库用户名
$dbpass=" http://www.yippeesoft.com "; //数据库密码
$dbname="my_db"; //数据库名
set_time_limit(0); //设置超时时间为0,表示一直执行。当php在safe mode模式下无效,此时可能会导致导入超时,此时需要分段导入
$fp = @fopen($file_name, "r") or die("不能打开SQL文件 $file_name");//打开文件
$link = mysql_pconnect(" http://www.yippeesoft.com ", "root", " http://www.yippeesoft.com ")
or die("Could not connect: " . mysql_error());
$result = mysql_query("CREATE DATABASE if not exists `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;")
or die("Invalid query: " . mysql_error());
mysql_connect($dbhost, $dbuser, $dbpass) or die("不能连接数据库 $dbhost");//连接数据库
mysql_select_db($dbname) or die ("不能打开数据库 $dbname");//打开数据库
echo "正在执行导入操作
";
while($SQL=GetNextSQL())&leftsign;
if (!mysql_query($SQL))&leftsign;
echo "<font color=\\"#ff0000\\">执行出错:".mysql_error()."</font>";
echo "SQL语句为:".$SQL."
";
&rightsign;;
&rightsign;
echo "导入完成";
fclose($fp) or die("Can\’t close file $file_name");//关闭文件
mysql_close();
//从文件中逐条取SQL
function GetNextSQL() &leftsign;
global $fp;
$sql="";
while ($line = @fgets($fp, 40960)) &leftsign;
$line = trim($line);
//以下三句在高版本php中不需要
//$line = str_replace("\\\\\\\\","\\\\",$line);
//$line = str_replace("\\\’","\’",$line);
//$line = str_replace("\\\\r\\\\n",chr(13).chr(10),$line);
// $line = stripcslashes($line);
if (strlen($line)>1) &leftsign;
if ($line[0]=="-" && $line[1]=="-") &leftsign;
continue;
&rightsign;
&rightsign;
$sql.=$line.chr(13).chr(10);
if (strlen($line)>0)&leftsign;
if ($line[strlen($line)-1]==";")&leftsign;
break;
&rightsign;
&rightsign;
&rightsign;
return $sql;
&rightsign;
?>
原创文章,转载请注明: 转载自YippeeSoft开心软件
本文链接地址: 20070518 php import sql txt
历史博文
- WPF splash screen - 2010
- 20080510 c# ParseExact StructLayout - 2009
- 0818 IIS 错误 系统找不到指定的路径 - 2007
- 1230 LINUX FTP gftp ncftp - 2006
- B2AFM_SF附件收集插件0.2.1.510 - 2005
- B2ATHADD附件压缩插件 - 2005
评论