当前位置:首页 » 编程语言 » php执行sql代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

php执行sql代码

发布时间: 2022-04-13 17:08:14

① PHP中如何执行sql语句

$sql = "select * from table";

$reault = mysql_query($sql);

print_r($result);

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

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

③ ThinkPHP里怎么直接执行一句SQL语句 - PHP框架开发

$waw
=
M();
$res
=
$waw->query($sql); 或
$res
=
$waw->execute($sql);
由于$sql中包含了表名,实例化模型时可以为空。
注:query()与execute()是有区别的,不能乱用,详见ThinkPHP光放手册驱动扩展部分。

褔递达

④ 如何在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查询怎么做

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

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

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

⑥ php执行 一段 sql语句

$name=addslashes($name);

insert into ** (name) values ('".$name."');

stringaddslashes ( string$str )
返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

⑦ php如何执行SQL语句

$conn=mysql_connect("localhost","root","icesky");//连接MYSQL

⑧ 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语句’))