PHP 匯入資料庫 sql 檔案

scutwang發表於2013-07-24

使用PHP 可以匯入sql來建立資料庫。程式碼如下:

<?php
  $hostname = 'localhost';
  $dbname = 'test';
  $username = 'root';
  $pw = '123456';
  $sqlfile = 'test.sql';
 
  $sql = file_get_contents($sqlfile);
  echo($sql); //輸出資料庫sql檔案的內容
  $conn = mysql_connect($hostname,$username,$pw) or die("無法連線資料庫");
  mysql_select_db($dbname,$conn) or die("資料庫連線失敗");
   
  //將sql檔案中的sql語句分解為一個個陣列成員,並執行
  $ar = split(";",join("",file($sqlfile)));
  foreach($ar as $v)
      mysql_query($v);
?>

 

相關文章