2013-01-24 1 views
0

내가이 오류정의되지 않은 방법 mysqli_stmt :: get_result() 내 서버에서

Fatal error: Call to undefined method mysqli_stmt::get_result() in /var/www/virtual/fcb/htdocs/library/mysqlidbclass.php on line 144 

을 얻을 그리고이 같은 래퍼 데 :

<?php 

/* https://github.com/aaron-lord/mysqli */ 

class mysqlidb { 
    /** 
    * Set up the database connection 
    */ 

    public function __construct($server,$user,$password,$db){ 
     $this->connection = $this->connect($server, $user, $password, $db, true); 
    } 


    /** 
    * Connect to the database, with or without a persistant connection 
    * @param String $host  Mysql server hostname 
    * @param String $user  Mysql username 
    * @param String $pass  Mysql password 
    * @param String $db   Database to use 
    * @param boolean $persistant Create a persistant connection 
    * @return Object    Mysqli 
    */ 
    private function connect($host, $user, $pass, $db, $persistant = true){ 
     $host = $persistant === true ? 'p:'.$host : $host; 

     $mysqli = new mysqli($host, $user, $pass, $db); 

     if($mysqli->connect_error) 
      throw new Exception('Connection Error: '.$mysqli->connect_error); 

     $mysqli->set_charset('utf8'); 

     return $mysqli; 
    } 

    /** 
    * Execute an SQL statement for execution. 
    * @param String $sql An SQL query 
    * @return Object  $this 
    */ 
    public function query($sql){ 
     $this->num_rows = 0; 
     $this->affected_rows = -1; 

     if(is_object($this->connection)){ 
      $stmt = $this->connection->query($sql); 

      # Affected rows has to go here for query :o 
      $this->affected_rows = $this->connection->affected_rows; 
      $this->stmt = $stmt; 
      return $this; 
     } 
     else { 
      throw new Exception; 
     } 
    } 

    /** 
    * Prepare an SQL statement 
    * @param String $sql An SQL query 
    * @return Object  $this 
    */ 
    public function prepare($sql){ 
     unset($this->stmt); 
     $this->num_rows = 0; 
     $this->affected_rows = -1; 

     if(is_object($this->connection)){ 
      # Ready the stmt 

      $this->stmt = $this->connection->prepare($sql); 

      if (false===$this->stmt) 
      { 
       print('prepare failed: ' . htmlspecialchars($this->connection->error)."<br />"); 
      } 

      return $this; 
     } 
     else { 
      throw new Exception(); 
     } 
    } 

    public function multi_query(){ } 

    /** 
    * Escapes the arguments passed in and executes a prepared Query. 
    * @param Mixed $var The value to be bound to the first SQL ? 
    * @param Mixed $... Each subsequent value to be bound to ? 
    * @return Object  $this 
    */ 
    public function execute(){ 
     if(is_object($this->connection) && is_object($this->stmt)){ 
      # Ready the params 
      if(count($args = func_get_args()) > 0){ 
       $types = array(); 
       $params = array(); 

       foreach($args as $arg){ 
        $types[] = is_int($arg) ? 'i' : (is_float($arg) ? 'd' : 's'); 
        $params[] = $arg; 
       } 

       # Stick the types at the start of the params 
       array_unshift($params, implode($types)); 

       # Call bind_param (avoiding the pass_by_reference crap) 
       call_user_func_array(
        array($this->stmt, 'bind_param'), 
        $this->_pass_by_reference($params) 
       ); 
      } 

      if($this->stmt->execute()){ 
       # Affected rows to be run after execute for prepares 
       $this->affected_rows = $this->stmt->affected_rows; 
       return $this; 
      } 
      else { 
       throw new Exception($this->connection->error); 
      } 
     } 
     else { 
      throw new Exception; 
     } 
    } 

    /** 
    * Fetch all results as an array, the type of array depend on the $method passed through. 
    * @param string $method  Optional perameter to indicate what type of array to return.'assoc' is the default and returns an accociative array, 'row' returns a numeric array and 'array' returns an array of both. 
    * @param boolean $close_stmt Optional perameter to indicate if the statement should be destroyed after execution. 
    * @return Array    Array of database results 
    */ 
    public function results($method = 'assoc', $close_stmt = false){ 
     if(is_object($this->stmt)){ 
      $stmt_type = get_class($this->stmt); 

      # Grab the result prepare() & query() 
      switch($stmt_type){ 
       case 'mysqli_stmt': 
        $result = $this->stmt->get_result(); 
        $close_result = 'close'; 
        break; 

       case 'mysqli_result': 
        $result = $this->stmt; 
        $close_result = 'free'; 
        break; 

       default: 
        throw new Exception; 
      } 

      $this->num_rows = $result->num_rows; 

      # Set the results type 
      switch($method) { 
       case 'assoc': 
        $method = 'fetch_assoc'; 
        break; 

       case 'row': 
        //return 'fetch_row'; 
        $method = 'fetch_row'; 
        break; 

       default: 
        $method = 'fetch_array'; 
        break; 
      } 

      $results = array(); 
      while($row = $result->$method()){ 
       $results[] = $row; 
      } 

      $result->$close_result(); 
      return $results; 
     } 
     else { 
      throw new Exception; 
     } 
    } 

    /** 
    * Turns off auto-committing database modifications, starting a new transaction. 
    * @return bool Dependant on the how successful the autocommit() call was 
    */ 
    public function start_transaction(){ 
     if(is_object($this->connection)){ 
      return $this->connection->autocommit(false); 
     } 
    } 

    /** 
    * Commits the current transaction and turns auto-committing database modifications on, ending transactions. 
    * @return bool Dependant on the how successful the autocommit() call was 
    */ 
    public function commit(){ 
     if(is_object($this->connection)){ 
      # Commit! 
      if($this->connection->commit()){ 
       return $this->connection->autocommit(true); 
      } 
      else { 
       $this->connection->autocommit(true); 
       throw new Exception; 
      } 
     } 
    } 

    /** 
    * Rolls back current transaction and turns auto-committing database modifications on, ending transactions. 
    * @return bool Dependant on the how successful the autocommit() call was 
    */ 
    public function rollback(){ 
     if(is_object($this->connection)){ 
      # Commit! 
      if($this->connection->rollback()){ 
       return $this->connection->autocommit(true); 
      } 
      else { 
       $this->connection->autocommit(true); 
       throw new Exception; 
      } 
     } 
    } 

    /** 
    * Return the number of rows in statements result set. 
    * @return integer The number of rows 
    */ 
    public function num_rows(){ 
     return $this->num_rows; 
    } 

    /** 
    * Gets the number of affected rows in a previous MySQL operation. 
    * @return integer The affected rows 
    */ 
    public function affected_rows(){ 
     return $this->affected_rows; 
    } 

    /** 
    * Returns the auto generated id used in the last query. 
    * @return integer The last auto generated id 
    */ 
    public function insert_id(){ 
     if(is_object($this->connection)){ 
      return $this->connection->insert_id; 
     } 
    } 

    /** 
    * Fixes the call_user_func_array & bind_param pass by reference crap. 
    * @param array $arr The array to be referenced 
    * @return array  A referenced array 
    */ 
    private function _pass_by_reference(&$arr){ 
     $refs = array(); 
     foreach($arr as $key => $value){ 
      $refs[$key] = &$arr[$key]; 
     } 
     return $refs; 
    } 
} 

?> 

을 너무 다른 기능을 사용하는 방법은 없나요 전체 앱을 다시 작성하지 않아도 되나요? 제발 말해줘.

+0

왜 mysqli 객체를 상속하고 그것을 확장하지 않는가? mysqli가 이미 OOP 인터페이스를 제공하기 때문에 자신 만의 객체 래퍼 클래스를 작성할 필요가 전혀 없다. –

+0

나는이 문제를 완벽하게 해결할 수있는 웹 사이트를 가지고있다. – Davit

+0

results() 메서드는보기 흉한 것처럼 보인다. 다른 결과 유형에 대해 별도의 메소드를 사용하지 않는 이유는 무엇입니까? 그리고 그들에게 스칼라와 행 배열을 되 돌리십시오. –

답변

0

이 오류의 이유는 서버가 '아무튼이다에서 추출 BIND_RESULT & FETCH

http://www.php.net/manual/en/mysqli-stmt.bind-result.php

http://www.php.net/manual/en/mysqli-stmt.fetch.php

mysqlnd 드라이버가 설치되어있다. (here을 참조하십시오.) 관리자 권한이있는 경우 직접 설치할 수도 있지만 더 쉬운 방법이 있습니다.

$stmt->get_result();과 동일한 기능을 제공하는 두 가지 간단한 함수를 작성했지만 mysqlnd는 필요하지 않습니다. 운전사.

당신은 단순히 $row = fetchRowAssoc($stmt, $fields);$fields = bindAll($stmt);

$row= $stmt->get_result();

$result = $stmt->get_result();를 교체합니다.

은 (당신이 $stmt->num_rows 사용할 수 있습니다 반환되는 행의 수를 얻을 수 있습니다.)

는 당신은 내가 당신의 PHP 스크립트 어딘가에 를 작성했습니다이 두 가지 기능 장소에 있습니다. (오른쪽 맨 아래에 예를 들어)

function bindAll($stmt) { 
    $meta = $stmt->result_metadata(); 
    $fields = array(); 
    $fieldRefs = array(); 
    while ($field = $meta->fetch_field()) 
    { 
     $fields[$field->name] = ""; 
     $fieldRefs[] = &$fields[$field->name]; 
    } 

    call_user_func_array(array($stmt, 'bind_result'), $fieldRefs); 
    $stmt->store_result(); 
    //var_dump($fields); 
    return $fields; 
} 

function fetchRowAssoc($stmt, &$fields) { 
    if ($stmt->fetch()) { 
     return $fields; 
    } 
    return false; 
} 

그것은 작동 방법 :

내 코드는 필드가 반환되는 얼마나 많은 및 파악하기 위해 $stmt->result_metadata(); 기능을 사용하여 자동으로 사전에 가져온 결과를 결합 창조 된 참고. 매력처럼 작동합니다!

또한 here에 게시되었습니다.

+1

몇 번이나이 답변을 복제하려고합니까? 그냥 궁금해서 –

+0

그런데 PHP 버전은 무엇입니까? –

+0

PHP 7.2 실행. –

관련 문제