文章目录

场景

  • 执行一个存储过程的时候,报错
Commands out of sync; you can't run this command now

复现


    $db->execute('BEGIN');
    $result = $db->multi_query("call SP_JIT_CALCULATE_SYN_STOCK(1526,0)");

    if (!$result) {
        $error_msg = $db->error_msg();
        echo "error_msg: " . $error_msg . PHP_EOL;
        $db->execute('ROLLBACK');

        return false;
    }

    $row = $db->query_result('select @sys_code,@sys_message');

    if ($row["@sys_code"] != 0 || $row["@sys_code"] === null){
        $error_msg = $row['@sys_message'];
        echo "error_msg2: " . $error_msg . PHP_EOL;
        return false;
    }

    $jitCalcSynStock = $db->fetch_array($result);
    $db->free_result($result);

    $db->execute('COMMIT');

分析

This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.
  • 很明显是连续执行multi_query query_result导致,在执行query_result之前执行mysql_free_result释放前一个query

修复

    $db->execute('BEGIN');
    $result = $db->multi_query("call SP_JIT_CALCULATE_SYN_STOCK(1526,0)");

    if (!$result) {
        $error_msg = $db->error_msg();
        echo "error_msg: " . $error_msg . PHP_EOL;
        $db->execute('ROLLBACK');

        return false;
    }

    $jitCalcSynStock = $db->fetch_array($result);
    $db->free_result($result);

    $row = $db->query_result('select @sys_code,@sys_message');

    if ($row["@sys_code"] != 0 || $row["@sys_code"] === null){
        $error_msg = $row['@sys_message'];
        echo "error_msg2: " . $error_msg . PHP_EOL;
        return false;
    }
    $db->execute('COMMIT');
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐