【问题标题】:Call mysql PROCEDURE inside while loop works only first time在while循环中调用mysql PROCEDURE只在第一次工作
【发布时间】:2013-08-09 11:07:46
【问题描述】:

我有一些来自我在表上发布的数据库的数据。对于表的每一行(while 循环的每个循环),我需要调用一个存储过程来计算我的表的最后一个<td> 的数据。

这里有一些代码:

$s = $lk->query("SELECT *
                  FROM A_USERS
                  JOIN A_DATA
                  WHERE A_DATA.id_user = A_USERS.id
                    AND A_USERS.usr_att = 1
                    AND A_DATA.act_data = 1
                    AND A_DATA.a_att = 1
                    AND A_DATA.qty IS NOT NULL
                  ORDER BY ID ASC");


while ($dato = $s->fetch_object()) {
    print '<tr>';

    print '<td>';   
    print $dato->id;
    print '</td>';

    //others cells


    $GetP = $lk->query("CALL GetPr($dato->qty, '$dato->prod')");

    if(mysqli_num_rows($GetP) === 1){
        while($p_db = mysqli_fetch_array($GetP)){
            $p = $p_db['p'];
        }

        $format = number_format(round($p, 1), 2, ',', '\'');

        //some other stuff here...

    }else{
        print '<td>';
        print 'ERROR';
        print '</td>';
    }
    print '</tr>';
}

问题是只有while 的第一个循环调用返回1 row 的过程。否则我在这个&lt;td&gt; 上总是得到ERROR,这意味着存储过程没有返回1 row

为什么?

编辑

存储过程:

CREATE DEFINER=`...` PROCEDURE `GetPr`(IN `cli_qty` INT, IN `cli_prod` VARCHAR(10) CHARSET ascii)
BEGIN
    SET @cli_qty = cli_qty;

    SET @q = (SELECT qty FROM MRG_H ORDER BY ABS(@cli_qty - qty) ASC LIMIT 1);

    SET @cli_prod = cli_prod;

    IF @cli_prod = 'OECO' THEN SET @marg = (SELECT (m+v) FROM MRG_H JOIN DT WHERE m <> '' AND q = @q AND par = 'MarOECO');

    ELSE SET @marg = (SELECT m FROM MRG_H WHERE m <> '' AND q = @q);
    END IF;

    SELECT (Med+@marg)*((Val+100)/100) AS P, D_aaaammgg AS Date
        FROM PL
          JOIN TVA
        WHERE I_D = 0
      AND Med <> ''
      AND Date_f = ''
        ORDER BY Iden DESC
        LIMIT 1;
END

MYSQLI 错误:

Error: 2014-Commands out of sync; you can't run this command now

【问题讨论】:

  • 程序运行良好。如果我用每一行的数据从PHP My Admin 调用它,它就可以工作!不管怎样,我现在发布它
  • 也许它不会为您的第一个查询中的任何其他行返回 1 行。你验证了吗?此外,无效数据可能是一个问题。 $dato-&gt;qty 可以是null$dato-&gt;prod 可以包含'null。在这种情况下,对过程的调用将变得无效。
  • 过程返回错误(如果我在CALL Procedure 之后添加or die(...),它就会死掉。$dato-&gt;qty 是一个整数,不能是NULL$dato-&gt;prod 只有两种可能性HELOECO
  • 您能否提供更多有关错误的详细信息。 mysqli_error 会返回一些东西吗?
  • 错误:2014-命令不同步;你现在不能运行这个命令

标签: php mysql loops stored-procedures procedure


【解决方案1】:

不确定这是问题所在,但是 mysqli_fetch_array 推进了内部指针,将其替换为 mysqli_num_rows。

while($p_db = mysqli_fetch_array($GetP)){
        $p = $p_db['p'];
    }

编辑:

http://www.php.net/manual/en/mysqli.query.php#102904

$s->close();

$lk->next_result();

把这个 2 放在 //other 单元格下面。

另外,将 fetch_object 替换为 fetch_all。

【讨论】:

  • Fatal error: Call to undefined method stdClass::close() in /.../test.php on line 130 现在是代码:print '&lt;/td&gt;'; $dato-&gt;close(); $s-&gt;next_result(); $GetP = $lk-&gt;query("CALL GetPr($dato-&gt;qty, '$dato-&gt;prod')");
  • Warning: mysqli_result::fetch_object() [mysqli-result.fetch-object]: Couldn't fetch mysqli_result in /.../test.php on line 94。这是第 94 行:while ($dato = $s-&gt;fetch_object()) { 这是正常的,因为我们关闭了数据
  • 好的解决方案已建立!我发布一个答案
【解决方案2】:

在@Mihai 的建议下,我找到了这个解决方案:

参考:PHP Manual

while ($dato = $s->fetch_object()) {
    print '<tr>';

    print '<td>';   
    print $dato->id;
    print '</td>';

    //others cells


    $GetP = $lk->query("CALL GetPr($dato->qty, '$dato->prod')");

    if(mysqli_num_rows($GetP) === 1){
        while($p_db = mysqli_fetch_array($GetP)){
            $p = $p_db['p'];
        }

        $format = number_format(round($p, 1), 2, ',', '\'');

        //some other stuff here...

    }else{
        print '<td>';
        print 'ERROR';
        print '</td>';
    }

    //Added this 2 lines
    $GetPrice->close();
    $link->next_result();

    print '</tr>';
}

【讨论】:

    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多