基于Php mysql存储过程的详解

 更新时间:2013年06月07日 10:06:35   作者:  
本篇文章是对Php中的mysql存储过程进行了详细的分析介绍,需要的朋友参考下
实例一:无参的存储过程
复制代码 代码如下:

$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的存储过程,看不到效果,可以在cmd下看到结果。

实例三:传出参数的存储过程
复制代码 代码如下:

$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的存储过程,在cmd下面看效果

实例五:使用变量的存储过程
复制代码 代码如下:

$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的存储过程,在cmd下面看效果

实例六: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的存储过程,在cmd下面看效果

实例七:循环语句
复制代码 代码如下:

$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的存储过程,在cmd下面看效果

实例八: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的存储过程,在cmd下面看效果

实例九: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的存储过程,在cmd下面看效果

实例十:删除存储过程
mysql_query("drop procedure if exists myproce");//删除test的存储过程
实例十一:存储过程中的游标
总结:
1.存储过程可用于InnoDB或MyISAM类型的表
2.show procedure status显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等<br>
3.SHOW CREATE PROCEDURE myproce显示某一个存储过程的详细信息<br>

相关文章

  • 浅谈mysql双层not exists查询执行流程

    浅谈mysql双层not exists查询执行流程

    本文主要介绍了浅谈mysql双层not exists查询执行流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • mysql二进制日志文件恢复数据库

    mysql二进制日志文件恢复数据库

    喜欢的在服务器或者数据库上直接操作的兄弟们你值得收藏下!不然你就悲剧了。-----(当然我也是在网上搜索的资料!不过自己测试通过了的!)
    2014-08-08
  • MySQL从MyISAM转换成InnoDB错误与常用解决办法

    MySQL从MyISAM转换成InnoDB错误与常用解决办法

    由于一些程序的要求,需要MyISAM数据引擎或InnoDB,下面是具体的解决方法,经测试偶尔会出现一些问题。
    2011-05-05
  • MySQL对相同字段创建不同索引解析

    MySQL对相同字段创建不同索引解析

    这篇文章主要为大家介绍了MySQL 对相同字段创建不同索引解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Mysql常用函数之Rank排名函数详解

    Mysql常用函数之Rank排名函数详解

    这篇文章主要介绍了Mysql常用函数之Rank排名函数详解,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • CentOS7.6安装MYSQL8.0的步骤详解

    CentOS7.6安装MYSQL8.0的步骤详解

    这篇文章主要介绍了CentOS7.6安装MYSQL8.0的步骤,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • mysql树目录查询语句优化提高查询效率

    mysql树目录查询语句优化提高查询效率

    在诸多的管理类,办公类等系统中,树形结构展示随处可见,本文主要介绍了mysql树目录查询语句优化提高查询效率的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • MySQL与JDBC之间的SQL预编译技术讲解

    MySQL与JDBC之间的SQL预编译技术讲解

    这篇文章主要介绍了MySQL与JDBC之间的SQL预编译技术讲解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • 详细讲解安全升级MySQL的方法

    详细讲解安全升级MySQL的方法

    这篇文章主要介绍了详细讲解安全升级MySQL的方法,在此特别推荐使用Percona Toolkit来辅助升级,当然,本文示例基于Linux环境,需要的朋友可以参考下
    2015-06-06
  • 分析Mysql大量数据导入遇到的问题以及解决方案

    分析Mysql大量数据导入遇到的问题以及解决方案

    这篇文章主要介绍了Mysql大量数据导入遇到的问题以及解决方案,希望我们整理的内容能够帮助到大家。
    2018-02-02

最新评论