php存储过程调用实例代码


Posted in PHP onFebruary 03, 2013
//比如要调用的存储过程为gxtj(a,b) 
$db=new mysqli("localhost","ssss","aaaaa","bbbb"); 
mysqli_query($db,"SET NAMES utf8"); 
$result=$db->query("call gxtj($year,$jd)"); // gxtj是mysql的存储过程名称 [color=gray][/color] 
while( $row = $result->fetch_array(MYSQLI_ASSOC)) //完成从返回结果集中取出一行 
{ 
while ($key=key($row)){ //依次取得字段名 
$value=current($row); //依次取得字段值 
} 
}

实例一:无参的存储过程

$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的存储过程
实例十:存储过程中的游标
总结中。

PHP 相关文章推荐
PHP时间戳使用实例代码
Jun 07 PHP
php面向对象全攻略 (七) 继承性
Sep 30 PHP
让PHP以ROOT权限执行系统命令的方法
Feb 10 PHP
php设计模式 Singleton(单例模式)
Jun 26 PHP
php 广告调用类代码(支持Flash调用)
Aug 11 PHP
PHP autoload与spl_autoload自动加载机制的深入理解
Jun 05 PHP
解析PHP可变函数的经典用法
Jun 20 PHP
PHP中定义数组常量(array常量)的方法
Nov 17 PHP
33道php常见面试题及答案
Jul 06 PHP
php实现微信公众号主动推送消息
Dec 31 PHP
浅谈PHP检查数组中是否存在某个值 in_array 函数
Jun 13 PHP
PHP实现多图上传(结合uploadify插件)思路分析
Nov 30 PHP
php中导出数据到excel时数字变为科学计数的解决方法
Feb 03 #PHP
php中删除字符串中最先出现某个字符的实现代码
Feb 03 #PHP
php数组去重的函数代码
Feb 03 #PHP
php中使用临时表查询数据的一个例子
Feb 03 #PHP
PHP应用JSON技巧讲解
Feb 03 #PHP
set_include_path和get_include_path使用及注意事项
Feb 02 #PHP
php代码中使用换行及(\n或\r\n和br)的应用
Feb 02 #PHP
You might like
php 无限分类的树类代码
2009/12/03 PHP
关于Iframe如何跨域访问Cookie和Session的解决方法
2013/04/15 PHP
PHP判断表单复选框选中状态完整例子
2014/06/24 PHP
PHP 中 var_export、print_r、var_dump 调试中的区别
2018/06/19 PHP
laravel5.1框架基础之Blade模板继承简单使用方法分析
2019/09/05 PHP
IE php关于强制下载文件的代码
2008/08/23 Javascript
防止页面被iframe(兼容IE,Firefox火狐)
2010/07/04 Javascript
Javascript Function对象扩展之延时执行函数
2010/07/06 Javascript
javascript跨域总结之window.name实现的跨域数据传输
2015/11/01 Javascript
Bootstrap入门书籍之(三)栅格系统
2016/02/17 Javascript
Actionscript与javascript交互实例程序(修改)
2016/09/22 Javascript
详解weex默认webpack.config.js改造
2018/01/08 Javascript
jquery.pagination.js分页使用教程
2018/10/23 jQuery
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
详解JWT token心得与使用实例
2019/08/02 Javascript
vue多页面项目中路由使用history模式的方法
2019/09/23 Javascript
用Python的Flask框架结合MySQL写一个内存监控程序
2015/11/07 Python
Python如何判断数独是否合法
2016/09/08 Python
python列表的增删改查实例代码
2018/01/30 Python
python tornado微信开发入门代码
2018/08/24 Python
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
2019/02/13 Python
Python 写入训练日志文件并控制台输出解析
2019/08/13 Python
Python3.6+selenium2.53.6自动化测试_读取excel文件的方法
2019/09/06 Python
Pytorch实验常用代码段汇总
2020/11/19 Python
山海经纬软件测试笔试题和面试题
2013/04/02 面试题
生产部岗位职责范文
2014/02/07 职场文书
关于美容院的活动方案
2014/08/14 职场文书
2014年班长个人工作总结
2014/11/14 职场文书
2014年优秀班主任工作总结
2014/12/16 职场文书
超市工作总结范文2014
2014/12/19 职场文书
员工旷工检讨书
2015/08/15 职场文书
初中教务主任竞聘演讲稿(范文)
2019/08/20 职场文书
python实现socket简单通信的示例代码
2021/04/13 Python
Nginx虚拟主机的配置步骤过程全解
2022/03/31 Servers
解决IIS7下无法绑定https主机的问题
2022/04/29 Servers
如何利用python创作字符画
2022/06/25 Python