1>通用分页存储过程代码及调用

/*****************************************************
MySQL分页存储过程
吴剑 2009-07-02  
*****************************************************/
DROP PROCEDURE IF EXISTS pr_pager;
CREATE PROCEDURE pr_pager(
    IN     p_table_name        VARCHAR(1024),        /*表名*/
    IN     p_fields            VARCHAR(1024),        /*查询字段*/
    IN     p_page_size         INT,                  /*每页记录数*/
    IN     p_page_now          INT,                  /*当前页*/
    IN     p_order_string      VARCHAR(128),         /*排序条件(包含ORDER关键字,可为空)*/  
    IN     p_where_string      VARCHAR(1024),        /*WHERE条件(包含WHERE关键字,可为空)*/    
    OUT    p_out_rows          INT                   /*输出记录总数*/    
)
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT '分页存储过程'
    
BEGIN

    /*定义变量*/
    DECLARE m_begin_row INT DEFAULT 0;
    DECLARE m_limit_string CHAR(64);

    /*构造语句*/    
    SET m_begin_row = (p_page_now - 1) * p_page_size;
    SET m_limit_string = CONCAT(' LIMIT ', m_begin_row, ', ', p_page_size);
    
    SET @COUNT_STRING = CONCAT('SELECT COUNT(*) INTO @ROWS_TOTAL FROM ', p_table_name, ' ', p_where_string);
    SET @MAIN_STRING = CONCAT('SELECT ', p_fields, ' FROM ', p_table_name, ' ', p_where_string, ' ', p_order_string, m_limit_string);

    /*预处理*/
    PREPARE count_stmt FROM @COUNT_STRING;
    EXECUTE count_stmt;
    DEALLOCATE PREPARE count_stmt;
    SET p_out_rows = @ROWS_TOTAL;

    PREPARE main_stmt FROM @MAIN_STRING;
    EXECUTE main_stmt;
    DEALLOCATE PREPARE main_stmt;
    
END;

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分页)