2012-08-17 3 views
3

MySQLi 용 래퍼 클래스를 작성하고 있습니다. 거기에서, 나는 질의와 변수 번호를 받아들이는 함수를 작성한다. mysqli_stmt::bind_param. 나는이 코드를 실행하면mysqli_stmt 객체에서 call_user_func_array를 사용할 수 없습니다.

<?php 
    $db = new DbHelper(); 
    $params = array('i', $stateID); 
    $result = $db->Execute('SELECT * FROM mst_cities WHERE State_ID = ?', $params);  
?> 

, 나는이 같은 경고를 얻을 : 여기

<?php 
    class DbHelper { 
     .... 
     public function Execute($query, $params){ 
      $this->open(); # Opens a connection to the database using MySQLi API 
      $stmt = $this->mysqli->prepare($query); 
      try{ 
       $result = call_user_func_array(array($stmt, 'bind_param'), $params); 
      } 
      catch(Exception $ex){ 
       # Handle Exception 
      } 
     } 
     .... 
    } 
?> 

I 함수 호출 오전 방법은 다음과 같습니다 여기에 코드가
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in......

을 내가 뭘해야 해야 할 것? docs 가입일

답변

4

:

참고 : call_user_func_array와 함께 mysqli_stmt_bind_param()를 사용하면

배려가 취해 져야한다(). mysqli_stmt_bind_param()은 참조로 전달되는 매개 변수를 요구하는 반면 call_user_func_array()는 참조 또는 값을 나타낼 수있는 변수 목록을 매개 변수로 사용할 수 있습니다.

예를 see this를 들어, 코멘트에 몇 가지 해결 방법이 있습니다

call_user_func_array(array($stmt, 'bind_param'), refValues($params)); 

function refValues($arr) 
{ 
    if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+ 
    { 
     $refs = array(); 
     foreach($arr as $key => $value) 
      $refs[$key] = &$arr[$key]; 
     return $refs; 
    } 
    return $arr; 
} 
+0

댄 감사합니다. 그게 효과가 있었어. –

+0

@ kush.impetus 당신은 그때 받아 들일 자유입니다 :) –

+0

또 다른 문제. '$ stmt-> execute(); 사용하기. $ meta = 'result_metadata();'그리고 $ meta ['num_rows ']'를 echo하는 것은 0을 반환하고'$ this-> mysqli-> affected_rows'를 echo하면 -1이 반환됩니다. 그렇지 않으면 쿼리를 직접 사용할 때 제대로 작동합니다. 무엇이 문제 일 수 있습니까? –

관련 문제