首页 > 开发 > PHP > 正文

php导入.sql文件到mysql数据库

2024-05-04 21:59:06
字体:大 中 小
来源:转载
供稿:网友
  1. set_time_limit(0); //设置超时时间为0,表示一直执行。当php在safe mode模式下无效,此时可能会导致导入超时,此时需要分段导入 
  2. $db =  new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],"utf8",$location['ztime']);   
  3. $fp = @fopen($sql, "r") or die("不能打开sql文件 $sql");//打开文件 
  4. while($sql=getnextsql()){ 
  5.     mysql_query($sql); 
  6. } 
  7. //echo "用户数据导入完成!"; 
  8. fclose($fp) or die("can't close file $file_name");//关闭文件 
  9. //从文件中逐条取sql 
  10. function getnextsql() { 
  11.     global $fp; 
  12.     $sql=""; 
  13.     while ($line = @fgets($fp, 40960)) { 
  14.         $line = trim($line); 
  15.         //以下三句在高版本php中不需要,在部分低版本中也许需要修改 
  16.         //$line = str_replace("//","/",$line); 
  17.         //$line = str_replace("'","'",$line); 
  18.         //$line = str_replace("/r/n",chr(13).chr(10),$line); 
  19.         //$line = stripcslashes($line); 
  20.         if (strlen($line)>1) { 
  21.             if ($line[0]=="-" && $line[1]=="-") { 
  22.                 continue; 
  23.             } 
  24.         } 
  25.         $sql.=$line.chr(13).chr(10); 
  26.         if (strlen($line)>0){ 
  27.             if ($line[strlen($line)-1]==";"){ 
  28.                 break;//开源代码Vevb.com 
  29.             } 
  30.         } 
  31.     } 
  32.     return $sql; 
  33. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表