php与mysql使用存储过程
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异步调试和线上调试网站程序的方法

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

MySQL 8.4(截至 2024 年的最新 LTS 版本)中引入的主要变化之一是默认情况下不再启用“MySQL 本机密码”插件。此外,MySQL 9.0完全删除了这个插件。 此更改会影响 PHP 和其他应用程序

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

PHP连接MySQL后页面空白,die()函数失效的原因分析在学习PHP和MySQL数据库连接的过程中,常常会遇到一些让人困惑...

CMS 代表内容管理系统。它是一种软件应用程序或平台,使用户能够创建、管理和修改数字内容,而无需先进的技术知识。 CMS 允许用户轻松创建和组织内容
