‘壹’ php批量执行sql语句怎么写
php中利用数组用Mysql_query批量执行SQL语句。
参考示例如下:
思路:这里采用一个数组.用explode 函数,将$query语句按照”;”炸开,然后循环执行即可:
$query = 'delete from ecs_goods_attr where attr_id=11 and goods_id=22;
Insert into ecs_goods_attr (goods_attr_id,goods_id,attr_id,attr_value,attr_price)values(Null,33,138,"胆略",0);
update ecs_goods set goods_number=10,shop_price=55 where goods_id=33;'
$query_e = explode(';','$query');
foreach ($query_e as $k =>$v)
{
mysql_query($query_e[$k]);
}
这样 $query语句就被批量的执行了。
‘贰’ 怎样在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语句
下次要是没把握时,先启动一个事务
象这样
begin
transaction
--启动一个事务
update
tablename
set
xxxxx
where
xxxx
select
*
from
tablename
--查看结果
--如果发现有问题就执行这个语句:
rollback
transaction
--没问题就迅速执行这个语句:
commit
transaction
这些都要先写好了,执行完成后要迅速执行事务提交或回滚语句。
以免启动事务影响其它人对更改过的表的访问。
‘肆’ php如何定时的执行sql语句
在mysql中创建事件,可用于定时执行
CREATE EVENT
CREATE EVENT [IF NOT EXISTS] event_name
ON SCHEDULE schele
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']
DO sql_statement;
‘伍’ PHP中如何执行sql语句
$sql = "select * from table";
$reault = mysql_query($sql);
print_r($result);
‘陆’ 如何在php中执行多条sql语句
这里没有很官方的解释,我个人认为mysql_query 不能批量执行SQL语句的原因最主要的一个就是Mysql_query不能判断你的批量语句里面是否包含错误.为了最大的保证程序的顺利执行,所以,干脆罢工了.
解决的办法,有很多种.这里仅列出我的心得之一(利用数组用Mysql_query批量执行SQL语句)
$query = ‘delete from ecs_goods_attr where attr_id=138 and goods_id=442;Insert into ecs_goods_attr (goods_attr_id,goods_id,attr_id,attr_value,attr_price)values(Null,442,138,”欧版 白色”,0);update ecs_goods set goods_number=10,shop_price=955 where goods_id=442;’
$query 是我需要执行的SQL语句,显然这里 mysql_query($query); 是无法得到我们想要的结果的.这里我们采用一个数组.用explode 函数,将$query语句按照”;”炸开.这个说的比较形象.呵呵
$query = ‘delete from ecs_goods_attr where attr_id=138 and goods_id=442;Insert into ecs_goods_attr (goods_attr_id,goods_id,attr_id,attr_value,attr_price)values(Null,442,138,”欧版 白色”,0);update ecs_goods set goods_number=10,shop_price=955 where goods_id=442;’
$query_e = explode(‘;’,’$query’);
foreach ($query_e as $k =>$v)
{
mysql_query($query_e[$k]);
}
这样 $query语句就被批量的执行了.呵呵
‘柒’ php 表单怎样执行sql语句
if($_GET['id']=='del'):
$del="delete from kbxx";
$res=mysql_query($del,$con);
exit;
endif;
echo'<a href="a.php?id=del">删除</a>';
‘捌’ php安装了xampp环境,怎么执行sql语句
sql文件属于一种已经导出的数据库备份文件,你需要将其导入数据库才能正常使用。
sql语句是:
use `数据库名`;
source xxx.sql;source后最好使用完整的文件路径例如“d:/xampp/xxx.sql”。
navicat中,直接打开数据库,然后右键点击数据库名,选择运行sql,执行即可。
‘玖’ ThinkPHP里怎么直接执行一句SQL语句 - PHP框架开发
$waw
=
M();
$res
=
$waw->query($sql); 或
$res
=
$waw->execute($sql);
由于$sql中包含了表名,实例化模型时可以为空。
注:query()与execute()是有区别的,不能乱用,详见ThinkPHP光放手册驱动扩展部分。
褔递达