调用MYSQL存储过程实例

PHP调用MYSQL存储过程实例

http://blog.csdn.net/ewing333/article/details/5906887 

http://www.cnblogs.com/kkcheng/archive/2010/03/19/1689672.html

http://hi.baidu.com/dreamontheway/item/8041f26ad5070131ad3e8346

实例一:无参的存储过程

复制代码
$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');
end; 
";
mysql_query($sql);//创建一个myproce的存储过程

$sql = "call test.myproce();";
mysql_query($sql);//调用myproce的存储过程,则数据库中将增加一条新记录。
复制代码

实例二:传入参数的存储过程

复制代码
$sql = "
create procedure myproce2(in score int)
begin
if score >= 60 then
select 'pass';
else
select 'no';
end if;
end; 
";
mysql_query($sql);//创建一个myproce2的存储过程
$sql = "call test.myproce2(70);";
mysql_query($sql);//调用myproce2的存储过程,看不到效果。
复制代码

实例三:传出参数的存储过程

复制代码
$sql = "
create procedure myproce3(out score int)
begin
set score=100;
end; 
";
mysql_query($sql);//创建一个myproce3的存储过程
$sql = "call test.myproce3(@score);";
mysql_query($sql);//调用myproce3的存储过程
$result = mysql_query('select @score;');
$array = mysql_fetch_array($result);
echo '<pre>';print_r($array);
复制代码

实例四:传出参数的inout存储过程

复制代码
$sql = "
create procedure myproce4(inout sexflag int)
begin
SELECT * FROM user WHERE sex = sexflag;
end; 
";
mysql_query($sql);//创建一个myproce4的存储过程
$sql = "set @sexflag = 1";
mysql_query($sql);//设置性别参数为1
$sql = "call test.myproce4(@sexflag);";
mysql_query($sql);//调用myproce4的存储过程
复制代码

实例五:使用变量的存储过程 

复制代码
$sql = "
create procedure myproce5(in a int,in b int)
begin
declare s int default 0;
set s=a+b;
select s;
end; 
";
mysql_query($sql);//创建一个myproce5的存储过程
$sql = "call test.myproce5(4,6);";
mysql_query($sql);//调用myproce5的存储过程
复制代码

实例六:case语法

复制代码
$sql = "
create procedure myproce6(in score int)
begin
case score
when 60 then select '及格';
when 80 then select '及良好';
when 100 then select '优秀';
else select '未知分数';
end case;
end; 
";
mysql_query($sql);//创建一个myproce6的存储过程
$sql = "call test.myproce6(100);";
mysql_query($sql);//调用myproce6的存储过程
复制代码

实例七:循环语句

复制代码
$sql = "
create procedure myproce7()
begin
declare i int default 0;
declare j int default 0;
while i<10 do
set j=j+i;
set i=i+1;
end while;
select j;
end; 
";
mysql_query($sql);//创建一个myproce7的存储过程
$sql = "call test.myproce7();";
mysql_query($sql);//调用myproce7的存储过程
复制代码

实例八:repeat语句

复制代码
$sql = " 
create procedure myproce8()
begin
declare i int default 0;
declare j int default 0;
repeat
set j=j+i;
set i=i+1;
until j>=10
end repeat;
select j;
end; 
";
mysql_query($sql);//创建一个myproce8的存储过程
$sql = "call test.myproce8();";
mysql_query($sql);//调用myproce8的存储过程
复制代码

实例九:loop语句

复制代码
$sql = "
create procedure myproce9()
begin
declare i int default 0;
declare s int default 0;

loop_label:loop
set s=s+i;
set i=i+1;
if i>=5 then
leave loop_label;
end if;
end loop;
select s;
end; 
";
mysql_query($sql);//创建一个myproce9的存储过程
$sql = "call test.myproce9();";
mysql_query($sql);//调用myproce9的存储过程
复制代码

实例十:删除存储过程 

mysql_query("drop procedure if exists myproce");//删除test的存储过程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 存储过程是一种封装了 SQL 语句的代码块,可以在 MySQL 数据库中创建、存储和调用存储过程可以接受输入参数、输出参数和返回值,可以使用分支、循环、异常处理等复杂的编程语言结构,适用于处理复杂的数据操作和业务逻辑。 下面是一个简单的 MySQL 存储过程实例: ```mysql DELIMITER // CREATE PROCEDURE sp_get_customer(IN customer_id INT, OUT customer_name VARCHAR(50)) BEGIN SELECT customer_name INTO customer_name FROM customers WHERE customer_id = customer_id; END // DELIMITER ; ``` 这个存储过程名为 `sp_get_customer`,有一个输入参数 `customer_id` 和一个输出参数 `customer_name`。存储过程的作用是根据传入的 `customer_id` 查询 `customers` 表中对应的客户名,并将其赋值给输出参数 `customer_name`。 使用以下语句调用这个存储过程: ```mysql CALL sp_get_customer(1, @customer_name); SELECT @customer_name; ``` 这个语句会查询 `customers` 表中 `customer_id` 为 1 的客户名,并将其赋值给变量 `@customer_name`,然后输出变量的值。 存储过程可以简化复杂的数据操作和业务逻辑,提高数据库的性能和安全性。但是,存储过程也有一些限制和注意事项: 1. 存储过程的语法比较复杂,需要熟悉 MySQL 的编程语言和语法规则。 2. 存储过程可能会影响数据库的性能和可维护性,应该谨慎使用和设计。 3. 存储过程可能会影响数据库的安全性,应该限制存储过程的访问权限和操作权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值