duande1146 2014-04-01 16:33
浏览 43
已采纳

如何在循环中执行多个语句

Hallo I have several queries to execute, all returning independent resultsets:

select * from table;
call procedureA(par1);
call procedureB(par2);

I would like to execute them within a loop to perform other operations:

$queries = array("select * from table;", "call procedureA(par1);", "call procedureB(par2);");
foreach($queries as $query) {
  $res=$db->query($query);
  // do something here with the query result
  someFunction($res);
}

The first statement runs fine; at the second iteration, it stops stating that $res is a non object:

Call to a member function ... on a non-object

Apart from using mysqli_multi_query(), in which way could I loop execute multiple queries?

UPDATE I removed $res->close(); from the code sample since it was misleading and ininfluent for the issue.

UPDATE2 - SOLUTION For anyone's sake, here is a complete working code:

$queries = array(
            "CALL procedureA(par1)"
            ,"CALL procedureB()"
            , "Select * from tableC"
            );

// Open connection
$db = new mysqli(
  $config['host']
  ,$config['user']
  ,$config['pwd']
  ,$config['dbname']
  ); 

foreach($queries as $query) {
  if ($res instanceof mysqli_result) {
      $res->free();
    }
  $res=$db->query($query);
  // do something with the query
  echo getHtmlTable($res);

  // free current result
  $res->free();

  // free subsequent resultset (es. the one with OK/ERR sp call status)
  while ($db->next_result()) {
    //free each result.
    $res = $db->use_result();
    if ($res instanceof mysqli_result) {
      $res->free();
    }
  }
}
  • 写回答

2条回答 默认 最新

  • duanluanlang8501 2014-04-01 17:49
    关注

    There is nothing special in running queries in a loop.

    There will be no difference, either if you write two queries just one after another, or run them in a loop. So, generally speaking, a couple of queries run in a loop is no different from a couple of queries run in order just like in all our scripts.

    The only possible problem a query itself. Say, a stored procedure call always return at least two resultsets. And no other query can be run until all resultsets get retrieved.

    So, as a quick-and-dirty solution a line like this could be added

    while ($db->next_result()) {}
    

    at the bottom of the loop to clean up all the resultsets possibly remained in a queue after the query execution.

    It qould be also highly convenient to turn on error reporting for mysqli, to make you aware of all the mysql error occurred. Having that, you would have added such error message to your question (Which is "Commands out of sync"). To do so, add this line before mysqli connect:

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题