php如何调用mysql存储过程,以下十个小案例抛砖引玉:
实例一:无参的存储过程
$conn = mysql_connect('localhost','root','root') or die ("connect db fail!"); 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 class="brush:php;toolbar:false">';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的存储过程
您可能感兴趣的文章
- mysql创建存储过程并在php中调用
- 用PHP函数memory_get_usage获取当前PHP内存消耗量以实现程序的性能优化
- php和mysql中uft-8中文编码乱码的几种解决办法
- php判断字符串是否全英文,纯中文,中英文组合的方法
- Mysql 数据库缓存cache功能分析,调试以及性能总结
- php和mysql中日期和unix时间戳的互相转换
- mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法
- php异步调试和线上调试网站程序的方法

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载
最流行的的开源编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),