当前位置:首页 » 编程语言 » 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 数据库名");