1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 error_reporting(E_ALL); 4 set_time_limit(0); 5 $file='./test.sql'; 6 $data=file($file); 7 8 echo "<pre>"; 9 //print_r($data); 10 $data_new=array(); 11 $tmp=array(); 12 foreach ($data as $line) { 13 14 $line=trim($line); 15 if(strlen($line)==0){ 16 continue; 17 } 18 if(substr($line,0,2)=='--'){ 19 continue; 20 } 21 if(substr($line,0,2)=='/*'){ 22 continue; 23 } 24 $tmp[]=$line; 25 if(substr($line,-1)==';'){ 26 $query=implode('',$tmp); 27 $tmp=array(); 28 $data_new[]=$query; 29 } 30 31 } 32 33 34 35 $mysqli=new mysqli('localhost','root','root','test'); 36 if($mysqli->connect_errno){ 37 exit('數據庫連接失敗!'); 38 } 39 $mysqli->query("set names utf8"); 40 41 $error=array(); 42 foreach($data_new as $sql){ 43 $mysqli->query($sql); 44 $r=$mysqli->error; 45 if($r) $error[]=$r; 46 } 47 print_r($r); 48 49 50 /* 51 52 53 mysql>ALTER TABLE tbl2 DISABLE KEYS; 54 Query OK, 0 rows affected (0.00 sec) 55 56 mysql>INSERT INTO tbl2 SELECT * FROM tbl1; 57 Query OK, 2000000 row affected (36.30 sec) 58 Records: 2000000 Duplicates: 0 Warnings: 0 59 60 mysql>ALTER TABLE tbl2 ENABLE KEYS; 61 Query OK, 0 rows affected (44.55 sec) 62 63 64 */
適用情況:phpmyadmin導出的sql文件過大,無法導入到線上phpmyadmin中
把該程序和sql文件上傳到空間中,用完後刪除即可。