TIP게시판

제목 mysql stored procedure 호출
글쓴이 nicengb 작성시각 2013/02/19 17:49:00
댓글 : 1 추천 : 1 스크랩 : 0 조회수 : 20276   RSS
안녕하세요. 
매번 질문만 하다가 mysql Stored Procedure(아래 SP) 호출 관련된 내용이 없는 것 같아 처음으로 tip을 남겨봅니다.
다들 이미 알고 계신것일수도 있지만 저처럼 입문하시는 분들을 위해서 ㅋ 
참고로 저는 mysqli 드라이버를 사용하고 있습니다. mysqli 드라이버 기준으로 작성하겠습니다.

저는 DB 쿼리를 할때 php에서 직접 쿼리를 만들어서 날리지 않고 미리 SP를 만들어 놓은 후에 php에서 해당 SP를 호출하는 형태로 사용하고 있습니다.
그런데 배포되는 CodeIgniter의 기본 mysqli 드라이버를 사용하면 일반 쿼리문은 잘 되지만 SP 호출시에는 "Commands out of sync" 와 같은 오류가 발생합니다.

이 오류를 해결하기 위해서는 system\database\drivers\의 mysqli_driver.php 를 아래와 같이 수정하면 됩니다.

*원래의 _execute 함수
function _execute($sql)
 {
  $sql = $this->_prep_query($sql);
  $result = @mysqli_query($this->conn_id, $sql);
  return $result;
 }

*수정한 _execute 함수
 function _execute($sql)
 {
  @mysqli_free_result($this->result_id);

  $sql = $this->_prep_query($sql);
  $retval = @mysqli_multi_query($this->conn_id, $sql);
  $firstResult = @mysqli_store_result($this->conn_id);

  while(@mysqli_more_results($this->conn_id)){   
   if (@mysqli_next_result($this->conn_id))
   {
    $result = @mysqli_use_result($this->conn_id);
    @mysqli_free_result($result);
   }
  }

  if( !$firstResult && !@mysqli_errno($this->conn_id)){
   return true;
  }

  return $firstResult;
 }

이와 같이 수정하면 일반 쿼리문과 SP호출을 모두 할 수 있습니다.
도움되시길 바랍니다.

감사합니다.


 다음글 lighttpd 웹서버 환경에서 index.php 지우... (2)
 이전글 csv 파일 업로드가 안될 때 (1)

댓글

루어홀릭 / 2015/01/13 15:31:30 / 추천 0
필요했던 자료네요. 감사합니다^^