实例解析MySQL中的存储过程及存储过程的调用方法

2019-01-04 12:25:10于丽

1.取记录调用:

call pr_pager('表名', '*', 25, 1, '', '', @count_rows); call pr_pager('user', '*', 15, 2, '', 'where id>3', @count_rows); call pr_pager('user', '*', 15, 1, 'group by password order by id desc', '', @count_rows);

2.调用1后再取条数调用:

select @count_rows; select @MAIN_STRING //select sql select @COUNT_STRING //seelct count sql

支持多表级联 ,分组 :

call pr_pager('job j left join enter_job ej on j.job_no=ej.job_no','j.*,ej.*','25','1','group by ej.put_away_user order by ej.put_away_user desc','where j.job_table="enter"',@p_out_rows);

<?php function dump_single_form41report($sys_report_id) { $this->dbConn->setFetchMode(DB_FETCHMODE_ASSOC); //SET @a=1;CALL dbpi_report.simpleproc(@a);SELECT @a; $sql = "CALL dbpi_temp.dumpSingleReportForm41($sys_report_id);"; $result = $this->dbConn->query($sql); if (mysql_error()) { die (mysql_error().'<b>:</b> dump_single_form41report(...)['.__LINE__.'];<br>'.$sql); } return $result; } function initQueuePool($sys_report_id, $username){ $this->dbConn->setFetchMode(DB_FETCHMODE_ASSOC); $this->checkPreviousThread($sys_report_id, $username); $temptablename = "_".$username."_".$sys_report_id; $sql = "SET @a=".$sys_report_id.";"; $this->dbConn->query($sql); $sql = "SET @b='".DB_REPORT.".".$temptablename."';"; $this->dbConn->query($sql); $sql = "SET @c='".DB_PREPRODUCT."';"; $this->dbConn->query($sql); $sql = "CALL ".DB_REPORT.".fm41_simpleproc(@a,@b,@c);"; $this->dbConn->query($sql); }

 
普通的查询,只返回一个结果集,而存储过程却返回至少两个结果集,其中一个就是存储过程的执行状态。我们必须要清除了这个执行状态以后,才可能再次调用另外一个存储过程 。

<?php $rs=mysql_query("call pr_pager('change_monitor','*',10,1,'','',@p_out_rows)"); while($rows=mysql_fetch_array($rs)){ echo $rows[Schedule]; } $query=mysql_query("select @p_out_rows"); $v=mysql_fetch_array($query); can't return a result set in the given context

需要php调用存储过程,返回一个结果集,发现很困难,找了半天,终于在老外的论坛上找到解决方案,这里本地化一下。
关键就是两点:
1.

define('CLIENT_MULTI_RESULTS', 131072);

2.

$link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());

您可能感兴趣的文章:

mysql 存储过程中变量的定义与赋值操作mysql存储过程详解mysql存储过程 游标 循环使用介绍MySQL存储过程例子(包含事务,输出参数,嵌套调用)MySql存储过程与函数详解mysql 查询数据库中的存储过程与函数的语句MySQL 有输入输出参数的存储过程实例Mysql存储过程和函数区别介绍MySQL 存储过程中执行动态SQL语句的方法mysql存储过程中使用游标的实例Mysql存储过程循环内嵌套使用游标示例代码mysql存储过程用法实例分析