溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php中怎么執(zhí)行多個存儲過程

發(fā)布時間:2021-07-09 17:06:28 來源:億速云 閱讀:105 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)php中怎么執(zhí)行多個存儲過程,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

從以前的使用原生代碼來看,只需要將結(jié)果集關(guān)閉即可,即

$this -> queryID -> close();

使用mysqli方式,修改DbMysqli.class.php,將query函數(shù)改為:

public function query($str) {
    $this -> initConnect(false);
    if (!$this -> _linkID) {
      return false;
    }
    $this -> queryStr = $str;
    //釋放前次的查詢結(jié)果
    if ($this -> queryID)
      $this -> free();
    N('db_query', 1);
    // 記錄開始執(zhí)行時間
    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();
    }
    //主要將這段移動了一下,關(guān)閉結(jié)果集
    if ($this -> _linkID -> more_results()) {
      while (($res = $this -> _linkID -> next_result()) != NULL) {
        $this -> queryID -> close();
      }
    }
    return $ret ;
}

下面就可以調(diào)用多個存儲過程,或許執(zhí)行其他SQL操作,可以直接使用M函數(shù)

在使用thinkphp的時候發(fā)現(xiàn)執(zhí)行多個存儲過程只能執(zhí)行第一個,看了一下源碼Driver/Db/DbMysql.class,已經(jīng)對存儲過程進行了一定處理,但是不知道為什么運行不了。

用原生代碼解決了問題(下面是部分代碼):

$db = new mysqli(C("DB_HOST"), C("DB_USER"), C("DB_PWD"), C("DB_NAME"));
if (mysqli_connect_errno())
throw_exception(mysqli_connect_error());
$t2 = microtime(true);
echo "數(shù)據(jù)庫連接用時:" . (($t2 - $t1)) . "s <br />";
$arr = array();
// 1st Query
$procedure = "call p1()";
$result = $db->query($procedure);
if ($result) {
// Cycle through results
while ($row = $result->fetch_object()) {
//添加到對象數(shù)組
$arr[] = $row;
}
// 這里是最重要的,需要將游標移動下一個結(jié)果集
$result->close();
$db->next_result();
}
$procedure = "call p2()";
$result = $db->query($procedure);
if ($result) {
// Cycle through results
while ($row = $result->fetch_object()) {
//添加到對象數(shù)組
$arr[] = $row;
}
// 這里是最重要的,需要將游標移動下一個結(jié)果集
$result->close();
$db->next_result();
}

看完上述內(nèi)容,你們對php中怎么執(zhí)行多個存儲過程有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI