2014年3月18日更新:
從以前的使用原生代碼來看,只需要將結果集關閉即可,即 $this -> queryID -> close(); 。
// 使用mysqli方式,修改DbMysqli.class.php,將query函數改為: public function query($str) { $this -> initConnect(false); if (!$this -> _linkID) { return false; } $this -> queryStr = $str; //釋放前次的查詢結果 if ($this -> queryID) $this -> free(); N('db_query', 1); // 記錄開始執行時間 G('queryStartTime'); $this -> queryID = $this -> _linkID -> query($str); // 對存儲過程改進 $ret = array(); $this -> debug(); if (false === $this -> queryID) { $this -> error(); return false; } else { $this -> numRows = $this -> queryID -> num_rows; $this -> numCols = $this -> queryID -> field_count; $ret = $this -> getAll(); } //主要將這段移動了一下,關閉結果集 if ($this -> _linkID -> more_results()) { while (($res = $this -> _linkID -> next_result()) != NULL) { $this -> queryID -> close(); } } return $ret ; }
下面就可以調用多個存儲過程,或許執行其他SQL操作,可以直接使用M函數
在使用thinkphp的時候發現執行多個存儲過程只能執行第一個,看了一下源碼Driver/Db/DbMysql.class,已經對存儲過程進行了一定處理,但是不知道為什么運行不了。
用原生代碼解決了問題(下面是部分代碼):
1 $db = new mysqli(C("DB_HOST"), C("DB_USER"), C("DB_PWD"), C("DB_NAME")); 2 if (mysqli_connect_errno()) 3 throw_exception(mysqli_connect_error()); 4 5 $t2 = microtime(true); 6 echo "數據庫連接用時:" . (($t2 - $t1)) . "s <br />"; 7 8 $arr = array(); 9 // 1st Query 10 $procedure = "call p1()"; 11 $result = $db->query($procedure); 12 if ($result) { 13 // Cycle through results 14 while ($row = $result->fetch_object()) { 15 //添加到對象數組 16 $arr[] = $row; 17 } 18 // 這里是最重要的,需要將游標移動下一個結果集 19 $result->close(); 20 $db->next_result(); 21 } 22 $procedure = "call p2()"; 23 $result = $db->query($procedure); 24 if ($result) { 25 // Cycle through results 26 while ($row = $result->fetch_object()) { 27 //添加到對象數組 28 $arr[] = $row; 29 } 30 // 這里是最重要的,需要將游標移動下一個結果集 31 $result->close(); 32 $db->next_result(); 33 }