PHP MsSQL(MS SQL 2012 express)自製資料庫還原程式(可以指定CSV將資料變成TABLE的內容)

php、MsSQL、MSSQL、express、header、odbc_connect、isset、fopen、odbc_num_fields、odbc_field_name、fwrite、fclose、odbc_close、odbc_free_result、trim、odbc_result

<?php 

  //http://www.programgo.com/article/48613569901/;jses...

  /**

  * @author samsun

  * @copyright 2007

  * php 使用ODBC连接sql server 数据库实例

  */ 

  header("Content-Type:text/html; charset=utf-8");//PHP 亂碼

  //數據庫配置信息

  $user=$_GET['user'];

  $pw=$_GET['pw'];

  $dbname=$_GET['dbname'];

  $tbname=$_GET['tbname'];

  set_time_limit(3600);//1hr time_out

  if(!isset($user) || !isset($pw) || !isset($dbname) || !isset($tbname))

  {

     die('參數不足,無法執行');

  }

  $server='127.0.0.1';

  /*

  $username='root'; 

  $password='700502'; 

  $database='v78'; 

  */

  $connstr = "Driver={SQL Server};Server=$server;Database=$dbname"; 

  $connect =odbc_connect($connstr,$user,$pw,SQL_CUR_USE_ODBC)or die ("couldn't connect");

  $sql = "truncate table ".$tbname;// 清空資料表: truncate table 資料表名稱;

  $result[0] = odbc_exec($connect,$sql);

  if ( !$result[0] )

  {

     die('無法執行清空資料表');

  }

  $filename = $dbname."_".$tbname.".csv";

  if(file_exists($filename))

  {

     $file = fopen($filename, "r");

     $index=0;

     while (!feof($file))

     {

       $str = fgets($file);

       if($str!='')//因為換行符號也算一行,所以雖然只有兩行資料,但是會有三行數據

       {

          if($index==0)

          {

            $field_name=trim($str,"\n");//取出欄位名稱並且刪除'\n'

          }

          else

          {

            $sql="INSERT INTO " .$tbname. "( ".$field_name." ) VALUES (".trim($str,"\n")." )";

            echo $sql;

            echo "<br>";

            $result[$index] = odbc_exec($connect,$sql);

          }

       }

       $index++;

     }

     fclose($file);  

  }

  else

  {

     die('還原檔案不存在');

  }

  odbc_close($connect);

  echo "MsSQL2CSV.php done...";

?>

參考來源:http://jashliao.pixnet.net/blog/post/206934322-php...


arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()