標籤:
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: 059 60 mysql>ALTER TABLE tbl2 ENABLE KEYS;61 Query OK, 0 rows affected (44.55 sec)62 63 64 */
適用情況:phpmyadmin匯出的sql檔案過大,無法匯入到線上phpmyadmin中
把該程式和sql檔案上傳到空間中,用完後刪除即可。
php 線上 mysql 大資料匯入程式