导航:首页 > 编程语言 > php执行sql代码

php执行sql代码

发布时间:2022-09-11 05:25:02

㈠ 如何利用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');
}

?>

㈡ PHP如何去执行一个SQL语句

下次要是没把握时,先启动一个事务
象这样
begin
transaction
--启动一个事务
update
tablename
set
xxxxx
where
xxxx
select
*
from
tablename
--查看结果
--如果发现有问题就执行这个语句:
rollback
transaction
--没问题就迅速执行这个语句:
commit
transaction
这些都要先写好了,执行完成后要迅速执行事务提交或回滚语句。
以免启动事务影响其它人对更改过的表的访问。

㈢ PHP中如何执行sql语句

$sql = "select * from table";

$reault = mysql_query($sql);

print_r($result);

㈣ 如何利用PHP执行.SQL文件

思路:用fopen打开a.sql文件,以回车为分隔符,循环的执行每一行的SQL语句就可以了!

㈤ 怎样在PHP里执行SQL脚本

先看个例子吧:
<?php
/*配置项*/
$mysql_server_name='localhost';
$mysql_username='root'; //用户名
$mysql_password='12345678'; //密码
$mysql_database='mycounter'; //数据库名
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //连接服务器
$sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; //sql语句创建数据库
';
mysql_query($sql); //此处执行SQL语句
$sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;';
//sql语句创建表
mysql_select_db($mysql_database,$conn); //连接数据库
$result=mysql_query($sql); //此处执行SQL语句
mysql_close($conn);
echo "Hello!数据库mycounter已经成功建立!";
?>

所以说在PHP里执行SQL脚本就是利用mysql_query(‘sql语句’)来执行的 (当然此处是只数据库是MYSQL的情况下,如过是sqlserver则是mssql_query(‘sql语句’))

㈥ PHP执行SQL查询怎么做

$haha = M(),$res = $haha->query($sql)。

或 $res = $waw->execute($sql)。

$sql中包含了表名,实例化模型时可以为空。注意query是查功能,execute是增删改功能。

㈦ PHP执行SQL查询

$haha = M(),$res = $haha->query($sql)。

或 $res = $waw->execute($sql)。

$sql中包含了表名,实例化模型时可以为空。注意query是查功能,execute是增删改功能。

㈧ PHP如何去执行一个SQL语句

<?php
$db_server="localhost";
$db_user="root";
$db_pwd="password";
$db_name="test";

$sql="select*fromuserwhereusername='admin'";
$conn=mysql_pconnect($db_server,$db_user,$db_pwd);
$my_db=mysql_select_db($db_name,$conn);
$result=mysql_query($sql,$conn);
$userInfo=mysql_fetch_array($result);
mysql_close($conn);

echo"帐户:".$userInfo["username"]."";
echo"密码:".$userInfo["password"]."";
echo"年龄:".$userInfo["userage"]."";
echo"等级:".$userInfo["usergrade"]."";
?>

阅读全文

与php执行sql代码相关的资料

热点内容
python超简单编程 浏览:257
获取命令方 浏览:976
怎样制作文件夹和图片 浏览:58
调研编译写信息 浏览:859
python冯诺依曼 浏览:417
同时安装多个app有什么影响 浏览:253
奥术杀戮命令宏 浏览:182
用sdes加密明文字母e 浏览:359
单片机原理及应用试题 浏览:423
易语言开启指定文件夹 浏览:40
马思纯参加密室大逃脱 浏览:322
文件夹冬季浇筑温度 浏览:712
京东有返点的aPp叫什么 浏览:603
如何查看u点家庭服务器是几兆 浏览:262
python应用接口怎么接 浏览:67
腐蚀怎么进不去服务器啊 浏览:359
linuxcpiogz 浏览:631
安卓中的布局是什么文件 浏览:397
dex反编译部分代码无法查看 浏览:464
linuxandroid编译 浏览:603