您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP操作Mysql存储过程

2024/3/16 8:20:52发布22次查看
mysql 存储过程是从 mysql 5.0 开始增加的新功能。存储过程的优点主要是执行效率和sql 代码封装。特别是 sql 代码封装功能,尤其是业务逻辑复杂的时候。现在有了 mysql 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高。
<?php /** * php操作mysql存储过程示例 * * @author flyer0126 * @date 2011-12-23 * */ //配置数据库连接信息 $hostname = 'localhost'; $username = '******'; $password = '******'; //连接mysql数据库 @$link = mysql_connect($hostname, $username, $password) or die("connect error:".mysql_error()); //选择目标库 mysql_select_db("flyer0126"); //展示目标库中数据表 @$res = mysql_list_tables("flyerdb"); for ($i=0; $i<mysql_num_rows($res); $i++){ echo mysql_tablename($res, $i)."<br/>"; } //创建存储结构 mysql_query("drop procedure if exists `my_test1`;", $link); $create_pro_sql = "create procedure `my_test1`(a int, b int) begin declare c int; if a is null then set a = 0; end if; if b is null then set b = 0; end if; set c = a + b; select c as sum; end;"; mysql_query($create_pro_sql, $link) or die("query invalid:".mysql_error()); //执行存储过程方式一 //$run_pro_sql = "call my_test1(1,2);"; //执行存储过程方式二 mysql_query("set @a = 1", $link); mysql_query("set @b = 2", $link); $run_pro_sql = "call my_test1(@a, @b);"; //执行存储过程 $result = mysql_query($run_pro_sql, $link) or die("query invalid:".mysql_error());; //获取返回值 $row = mysql_fetch_row($result); echo $row[0]; //3 //关闭连接 mysql_close($link); ?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product