php+mysql将大数据sql文件导入数据库

数据库   发布日期:2025年05月27日   浏览次数:181
  1. <?php
  2. $file_name = "d:test.sql";
  3. $dbhost = "localhost";
  4. $dbuser = "root";
  5. $dbpass = "";
  6. $dbname = "test";
  7. set_time_limit();
  8. $fp = @fopen($file_name,"r") or die("sql文件打不开");//打开文件
  9. $pdo = new PDO("mysql:host=localhost;dbname=test","root","");//连接数据库
  10. $pdo->query('set names utf8');//设置编码
  11. echo "正在执行导入操作";
  12. while($SQL = GetNextSQL()){
  13. if(!$pdo->query($SQL)){
  14. echo "执行出错";
  15. echo "SQL语句为".$SQL;
  16. }
  17. }
  18. echo "导入完成";
  19. fclose($fp) or die("can't close file");//关闭文件
  20. mysql_close();
  21. //从文件中逐条取sql
  22. function GetNextSQL(){
  23. global $fp;
  24. $sql="";
  25. while($line = @fgets($fp,)){
  26. $line = trim($line);
  27. $line = str_replace("////", "//", $line);
  28. $line = str_replace("/","'",$line);
  29. $line = str_replace("//r//n","chr(13).chr(10)",$line);
  30. $line = stripcslashes($line);
  31. if(strlen($line)>){
  32. if($line[]=='-' && $line[]=="-"){
  33. continue;
  34. }
  35. }
  36. $sql .= $line.chr().chr();
  37. if(strlen($line)>){
  38. if($line[strlen($line)-]==";"){
  39. break;
  40. }
  41. }
  42. }
  43. return $sql;
  44. }

 

以上就是php+mysql将大数据sql文件导入数据库的详细内容,更多关于php+mysql将大数据sql文件导入数据库的资料请关注九品源码其它相关文章!