php存储过程调用实例代码_php技巧

复制代码 代码如下:

//比如要调用的存储过程为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的存储过程
实例十:存储过程中的游标
总结中。

时间: 2024-08-02 19:32:17

php存储过程调用实例代码_php技巧的相关文章

PHP调用Webservice实例代码_php技巧

它是一个开源软件,是完全采用PHP语言编写的.通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )开发.NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,不受服务器安全设置的影响. 方法一:直接调用 复制代码 代码如下: <? include('NuSoap.php'); // 创建一个soapclient对象,参数是server的WSDL $c

php分页函数完整实例代码_php技巧

本文分享一例php分页函数完整实例代码,使用此函数实现分页效果很不错.分享给大家供大家参考. 具体功能代码如下: <?php /* * Created on 2011-07-28 * 使用方法: require_once('mypage.php'); $result=mysql_query("select * from mytable", $myconn); $total=mysql_num_rows($result); //取得信息总数 pageDivide($total,10

php函数连续调用实例分析_php技巧

本文实例讲述了php函数连续调用的方法.分享给大家供大家参考.具体如下: <?php //返回$this,实现连续调用 class xin { function name($namec) { echo "我的名字是:$namec <br/>"; return $this; } function age($agec) { echo "我的年龄是:$agec <br/>"; return $this; } } $xind = new xin

PHP正则匹配日期和时间(时间戳转换)的实例代码_php技巧

先来一个比较简单实用的代码 日期YYYY-MM-DD $str = ''; $isMatched = preg_match('/^\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}$/', $str, $matches); var_dump($isMatched, $matches); php需要一定的时间格式才能转换成时间戳(表示从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数),这就要用到php正则判断,以下是代码: <?php //匹配时间格式为2016-0

php获取本机真实IP地址实例代码_php技巧

本文实例为大家分享了php获取本机真实IP地址实例代码,供大家参考. 主要是获取操作系统为win2000/xp.win7的本机IP真实地址,和获取操作系统为linux类型的本机IP真实地址,具体内容如下 function getLocalIP() { $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/

php+ajax无刷新上传图片实例代码_php技巧

本文分享了php结合ajax实现无刷新上传图片的实例代码,分享给大家,希望大家可以和小编一起学习学习,共同进步. 1.引入文件 <!--图片上传begin--> <script type="text/javascript" src="/js/jquery.form.js"></script> <script type="text/javascript" src="/js/uploadImg.js

PHP实现删除非站内外部链接实例代码_php技巧

一般在做网站系统的时候,出于优化等因素的考虑需要再添加文章的时候删除掉不是本站的链接,对于这一要求可以通过让PHP处理下文章内容,来达到文章外部链接的自动删除的效果. 本实例代码主要参考织梦CMS内容管理系统的外链删除方法. 复制代码 代码如下: /** *  删除非站内链接 * * @access    public * @param     string  $body  内容 * @param     array  $allow_urls  允许的超链接 * @return    strin

PHP调用MySQL的存储过程的实现代码_php技巧

MySQL好像从5.0开始才引入存储过程,反正以前做应用的时候从没碰过,不过现在因为主要作内部系统,所以很多应用都用到了存储过程,当然前台有时候也需要调用MySQL存储过程,PHP的MySQL Lib好像支持的不是很好,不过我搜索了些资料,虽然不多,但是还是尝试的使用了,现在介绍一下方法,以便用到的朋友不用再头疼. lMySQL扩展也是支持存储过程的,不过只支持无返回结果的存储过程,如果该存储过程存在输出,这个调用就会抛出一个错误,具体错误忘记了.调用方式很简单: $rs = mysql_que

2款PHP无限级分类实例代码_php技巧

本文章总结了两款PHP无限级分类实现程序代码,有需要学习的朋友可参考一下.主要思路:首先看第三行和第四行,父类ID(PARENTID)的值是1,表示属于id=1这个类的子类,而,一,二两行因为是一级分类,没有上级分类,所以父类ID(PARENTID)的值是0,表示初级分类,依次类推便实现了无限级分类.最终的效果是:├一级分类A ├─┴二级分类A ├─┴二级分类B ├一级分类B然后就是程序,这里以PHP作为描述语言,可以很方便的改成其他语言,因为原理相似,就是一个递归而已. <?php $dbho