0

我又来这里再次寻求帮助,我已经在 mysql 中创建了一个存储过程,当在 SQLyog 或 MySQL Workbench 之类的 sgb 上调用它时,我通常会检索我的查询结果,但是当我做同样的事情时在 php 上执行 mysql_query,什么也没给出。

这是我执行 mysql 查询的函数。

function conect($SERVER){
        if (strtoupper($SERVER) =='MYSQL') {
        $this->con=mysql_connect(SQL_SERVER,SQL_USER,SQL_PASSWD)or die(mysql_error());
        $this->db=mysql_select_db(SQL_DB,$this->con);
            if ($this->con == 0){
                $retorno = "CONECTION ERROR - SERVER!<br>";
            }
            else if ($this->db == 0){
                $retorno = "CONECTION ERROR  - DATA BASE!<br>";
            } else {
                $retorno = "";
            }

            return  $retorno;

        }

    }

    function isConected(){
        if($this->con == false)
            return false;
        return true;
    }

    function execute($qry, $res="load"){
        if(!$this->isConected())
            $this->conect('MYSQL');

        $this->result[$res] = mysql_query($qry, $this->con) or trigger_error(mysql_error() . ": \n" . __FILE__ . ": \n" . $res . " - " . $qry);
        if($this->result[$res])return true;
        trigger_error(mysql_error());
        return false;
    }

    function get_fetch_assoc($res="load"){
        return mysql_fetch_assoc($this->result[$res]);
    }

这是我的程序调用

$mysql = new conexao();

            $qry = "CALL spGetChamadoMaisAntigo('".$uareas."', '".$_SESSION["dpto_codigoUrl"]."')";

            $mysql->execute($qry, 'Load');

            while($row = $mysql->get_fetch_assoc('Load')){
                $chamadosAbilitados[] = $row;
            }

我已经使用一个简单的查询测试了这个函数

从表名中选择 *

它正常返回结果。

这是我的程序

DELIMITER $$

DROP PROCEDURE IF EXISTS `ocomon_rc6`.`spGetChamadoMaisAntigo`$$

CREATE DEFINER=`renancr`@`%` PROCEDURE `spGetChamadoMaisAntigo`(IN Sistema VARCHAR(1000),IN CodigoUrl INT)
BEGIN
    CALL lib_Explode( ',' ,  Sistema );
    DROP TABLE IF EXISTS ocomon_rc6.TempChamados;

    CREATE TABLE IF NOT EXISTS ocomon_rc6.TempChamados(numero INT, dias_apos_abertura INT);
    INSERT INTO TempChamados(numero, dias_apos_abertura)
    SELECT 
        o.numero,
        (CASE
            WHEN CONVERT(NOW(), TIME) > CONVERT(o.data_abertura, TIME) THEN DATEDIFF(NOW(), o.data_abertura)
            ELSE DATEDIFF(DATE_SUB(NOW(),INTERVAL 1 DAY), o.data_abertura)
        END) 
    FROM 
        ocorrencias as o 
    LEFT JOIN 
        sistemas AS a ON 
            a.sis_id = o.sistema 
    LEFT JOIN 
        sistemas_x_url AS su ON 
            a.sis_id = su.sis_id 
    LEFT JOIN 
        urls ON 
            su.codigoUrl = urls.codigoUrl 
    LEFT JOIN 
        localizacao AS l ON 
            l.loc_id = o.local 
    LEFT JOIN 
        instituicao AS i ON 
            i.inst_cod = o.instituicao 
    LEFT JOIN 
        usuarios AS u ON 
            u.user_id = o.operador 
    LEFT JOIN 
        usuarios AS ua ON 
            ua.user_id = o.aberto_por 
    LEFT JOIN 
        `status` AS s ON 
            s.stat_id = o.status 
    LEFT JOIN 
        status_categ AS stc ON 
            stc.stc_cod = s.stat_cat 
    LEFT JOIN 
        problemas AS p ON 
            p.prob_id = o.problema 
    LEFT JOIN 
        sla_solucao AS sls ON 
            sls.slas_cod = p.prob_sla 
    LEFT JOIN 
        prioridades AS pr ON 
            pr.prior_cod = l.loc_prior 
    LEFT JOIN 
        sla_solucao AS slr ON 
            slr.slas_cod = pr.prior_sla 
    LEFT JOIN 
        script_solution AS sol ON 
            sol.script_cod = o.oco_script_sol 
    LEFT JOIN 
        prior_atend AS prioridade_atendimento ON 
            prioridade_atendimento.pr_cod = o.oco_prior 
    LEFT JOIN 
        sistemas_x_filtro AS filtr ON 
            filtr.codigoSistemaFiltro = o.filtro 
    WHERE 
        s.stat_painel IN (2) 
    AND o.sistema IN (SELECT val FROM lib_Explode) 
    AND su.codigoUrl = CodigoUrl
    AND o.oco_scheduled = 0
    ORDER BY 
        o.data_abertura;
    DROP TABLE IF EXISTS lib_Explode;
    SELECT * FROM TempChamados WHERE dias_apos_abertura = (SELECT MAX(dias_apos_abertura) FROM TempChamados);

    DROP TABLE IF EXISTS ocomon_rc6.TempChamados;
    END$$

DELIMITER ;

有人知道我做错了什么吗?

4

1 回答 1

0

解决了,我分析了本教程http://www.joeyrivera.com/2009/using-mysql-stored-procedures-with-php-mysqlmysqlipdo/它给了我一个答案,需要声明它需要发送到的所有参数php,并将select的结果插入到这个参数中,我已经修改了我的过程,并用@调用了这个输出参数的过程,最后用@做了一个简单的选择这个输出参数,如下所示。

更改了程序的参数

DROP PROCEDURE IF EXISTS `ocomon_rc6`.`spGetChamadoMaisAntigo`$$

CREATE DEFINER=`renancr`@`%` PROCEDURE `spGetChamadoMaisAntigo`(IN Sistema VARCHAR(1000),IN CodigoUrl INT, OUT numero INT, OUT diasAposAbertura INT)

更改了内部选择并将其插入到输出参数中

SELECT idOcorrencia, dias_apos_abertura INTO numero , diasAposAbertura FROM TempChamados WHERE dias_apos_abertura = (SELECT MAX(dias_apos_abertura) FROM TempChamados);

在 php 中,我更改了如下所示的过程调用。

$qry = "CALL spGetChamadoMaisAntigo('".$uareas."', '".$_SESSION["dpto_codigoUrl"]."', @numero , @diasAposAbertura)";
$mysql->execute($qry, 'Load');

最后我选择了这个输出参数,如下所示。

$qry = "SELECT @numero , @diasAposAbertura";
$mysql->execute($qry, 'Load');
于 2013-05-21T10:49:41.083 回答