當前位置:首頁 » 編程語言 » php將記錄生成SQL文件
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

php將記錄生成SQL文件

發布時間: 2022-12-29 19:08:12

A. 如何利用PHP執行.sql文件

以下代碼來自PHPBB的SQL文件解析類,原理就是逐行的解釋SQL文件,並進行刪除等操作,可以參照修改。希望能幫到你。

<?php
ini_set('memory_limit','5120M');
set_time_limit(0);
/***************************************************************************
*sql_parse.php
*-------------------
*begin:ThuMay31,2001
*right:(C)2001ThephpBBGroup
*email:[email protected]
*
*$Id:sql_parse.php,v1.82002/03/1823:53:12psotfxExp$
*
****************************************************************************/

/***************************************************************************
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*
*theFreeSoftwareFoundation;eitherversion2oftheLicense,or
*(atyouroption)anylaterversion.
*
***************************************************************************/

/***************************************************************************
*
*_utilitiesundertheadmin
*,specifically
*
*functionsintothisfile.JLH
*
***************************************************************************/

//
//remove_
//....
//
functionremove_comments(&$output)
{
$lines=explode(" ",$output);
$output="";

//trytokeepmem.usedown
$linecount=count($lines);

$in_comment=false;
for($i=0;$i<$linecount;$i++)
{
if(preg_match("/^/*/",preg_quote($lines[$i])))
{
$in_comment=true;
}

if(!$in_comment)
{
$output.=$lines[$i]." ";
}

if(preg_match("/*/$/",preg_quote($lines[$i])))
{
$in_comment=false;
}
}

unset($lines);
return$output;
}

//
//remove_
//
functionremove_remarks($sql)
{
$lines=explode(" ",$sql);

//trytokeepmem.usedown
$sql="";

$linecount=count($lines);
$output="";

for($i=0;$i<$linecount;$i++)
{
if(($i!=($linecount-1))||(strlen($lines[$i])>0))
{
if(isset($lines[$i][0])&&$lines[$i][0]!="#")
{
$output.=$lines[$i]." ";
}
else
{
$output.=" ";
}
//Tradingabitofspeedforlowermem.usehere.
$lines[$i]="";
}
}

return$output;

}

//
//split_sql_.
//Note:expectstrim()tohavealreadybeenrunon$sql.
//
functionsplit_sql_file($sql,$delimiter)
{
//Splitupourstringinto"possible"SQLstatements.
$tokens=explode($delimiter,$sql);

//trytosavemem.
$sql="";
$output=array();

//wedon'.
$matches=array();

//thisisfasterthancallingcount($oktens)everytimethrutheloop.
$token_count=count($tokens);
for($i=0;$i<$token_count;$i++)
{
//Don'.
if(($i!=($token_count-1))||(strlen($tokens[$i]>0)))
{
//.
$total_quotes=preg_match_all("/'/",$tokens[$i],$matches);
//,
//whichmeansthey'reescapedquotes.
$escaped_quotes=preg_match_all("/(?<!\\)(\\\\)*\\'/",$tokens[$i],$matches);

$unescaped_quotes=$total_quotes-$escaped_quotes;

//,.
if(($unescaped_quotes%2)==0)
{
//It'sacompletesqlstatement.
$output[]=$tokens[$i];
//savememory.
$tokens[$i]="";
}
else
{
//incompletesqlstatement..
//$tempwillholdwhatwehavesofar.
$temp=$tokens[$i].$delimiter;
//savememory..
$tokens[$i]="";

//Dowehaveacompletestatementyet?
$complete_stmt=false;

for($j=$i+1;(!$complete_stmt&&($j<$token_count));$j++)
{
//.
$total_quotes=preg_match_all("/'/",$tokens[$j],$matches);
//,
//whichmeansthey'reescapedquotes.
$escaped_quotes=preg_match_all("/(?<!\\)(\\\\)*\\'/",$tokens[$j],$matches);

$unescaped_quotes=$total_quotes-$escaped_quotes;

if(($unescaped_quotes%2)==1)
{
//oddnumberofunescapedquotes.
//statement(s),wenowhaveacompletestatement.(2oddsalwaysmakeaneven)
$output[]=$temp.$tokens[$j];

//savememory.
$tokens[$j]="";
$temp="";

//exittheloop.
$complete_stmt=true;
//.
$i=$j;
}
else
{
//evennumberofunescapedquotes.Westilldon'thaveacompletestatement.
//(1oddand1evenalwaysmakeanodd)
$temp.=$tokens[$j].$delimiter;
//savememory.
$tokens[$j]="";
}

}//for..
}//else
}
}

return$output;
}

$dbms_schema='yourfile.sql';

$sql_query=@fread(@fopen($dbms_schema,'r'),@filesize($dbms_schema))ordie('problem');
$sql_query=remove_remarks($sql_query);
$sql_query=split_sql_file($sql_query,';');

$host='localhost';
$user='user';
$pass='pass';
$db='database_name';

mysql_connect($host,$user,$pass)ordie('errorconnection');
mysql_select_db($db)ordie('errordatabaseselection');

$i=1;
foreach($sql_queryas$sql){
echo$i++;
echo"
";
mysql_query($sql)ordie('errorinquery');
}

?>

B. php如何將sql語句如何保存到.sql文檔

如果你指的是將資料庫的操作SQL語句,保存為.sql文檔,那麼可以試著使用 phymyadmin ,可以完成完整的建立資料庫、表、查詢等操作。

C. php 導出 欄位 內容 為sql文件

phpmyadmin 導出的時候選一下另存為_DB_文件 ,導出來的就是.sql文件了

D. sql如何生成sql文件

材料/工具:

1、打開資料庫

E. 如何利用PHP執行.SQL文件

其實很簡單,就是獲取sql文件中的內容,然後將每一句sql語句一次執行就行啦。
這是代碼

//讀取文件內容
$_sql = file_get_contents('test.sql');

$_arr = explode(';', $_sql);
$_mysqli = new mysqli(DB_HOST,DB_USER,DB_PASS);
if (mysqli_connect_errno()) {
exit('連接資料庫出錯');
}
//執行sql語句
foreach ($_arr as $_value) {
$_mysqli->query($_value.';');
}
$_mysqli->close();
$_mysqli = null;

上面text.sql是你需要執行的sql文件,DB_HOST主機名,DB_USER用戶名,DB_PASS密碼!
這只是最基本的自動執行sql文件,你還可以自定義生成資料庫的名稱,方法就是將sql文件中下面的代碼刪去

1
2
CREATE DATABASE IF NOT EXISTS 資料庫名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE 資料庫名

然後在text.php中執行所有的sql語句前添加代碼
$_mysqli->query("CREATE DATABASE IF NOT EXISTS 資料庫名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;");
$_mysqli->query("USE 資料庫名");