MySQL调用函数中的“命令不同步”

时间:2015-07-08 08:24:12

标签: php mysql mysqli

我正在尝试清理我的PHP,特别是MySQL SP调用。我希望能够通过简单的函数调用来执行我的存储过程,如:

    $result = run_procedure("sp_test(2)");

我尝试了很多aproaches,但每次遇到命令都不同步;您现在无法运行此命令 - 多次执行此功能时出错。 这是我的一次尝试。看看我如何将结果集的内容复制到数组中并在返回数组之前释放结果集?为什么我仍然会收到错误?

function run_procedure($procedure_string) {
   open_connection(); // A seperate func that opens the global $db_connection;

   $procedure_result = mysqli_query($GLOBALS["db_connection"], "CALL " . $procedure_string,MYSQLI_STORE_RESULT);
   if (mysqli_errno($GLOBALS["db_connection"])) {
      die("Query fail: " . mysqli_error($GLOBALS["db_connection"]));
   }
   $result_array = array();
   while($row = mysqli_fetch_assoc($procedure_result)) {
      $result_array[] = $row;
   }
   mysqli_free_result($procedure_result);
   return $result_array;
}

我真的更喜欢只返回结果集(而不是在这里构建数组),但我也不能创建那个世界。

有什么好主意吗?有人使用单独的函数进行MySQLi执行吗?

0 个答案:

没有答案
相关问题