mysqli_free_result

(PHP 5)

mysqli_free_result

(no version information, might be only in CVS)

result->free -- Frees the memory associated with a result

Description

Procedural style:

void mysqli_free_result ( mysqli_result result )

Object oriented style (all methods are equivalent):

class mysqli_result {

void free ( void )

void close ( void )

void free_result ( void )

}

The mysqli_free_result() function frees the memory associated with the result represented by the result parameter, which was allocated by mysqli_query(), mysqli_store_result() or mysqli_use_result().

Замечание: You should always free your result with mysqli_free_result(), when your result object is not needed anymore.

Возвращаемые значения

This function doesn't return any value.



mysqli_free_result
lucky760 at yahoo dot com
14-Jul-2006 01:37
Replace the switch statement in __get() of my previous post with these two lines:
<?php
//if (property_exists('mysqli_result', $i_varname))// <-- PHP5.1.0RC1 required
 
return $this->m_mysqli_result->{$i_varname};
?>

I wasn't aware of that syntax until just now. It's much more attractive.
lucky760 at yahoo dot com
14-Jul-2006 01:26
Here's a method I wrote to simplify the automatic freeing of mysqli_result objects when all references disappear. The beauty of it is that it's presence is tranparent to the user because it acts exactly like a mysqli_result object.

Also, jon at fuck dot org, you don't have to pass the resultmode ("flag") around because it's accessible with the mysqli_result object (the 'type' property).

<?php
final class MysqliResultWrapper {
  private
$m_mysqli_result;

  public function
__construct($i_result) {
   if (isset(
$i_result) && $i_result instanceof mysqli_result)
    
$this->m_mysqli_result = $i_result;
   else
     throw new
Exception(__CLASS__ . ' instantiated with ' . gettype($i_result));
  }
 
 
// automatically disappear the mysqli_result when all references to us vanish
 
public function __destruct() {
   if (isset(
$this->m_mysqli_result))
    
$this->m_mysqli_result->free();
  }

 
// pass back mysqli_result properties
 
public function __get($i_varname) {
   if (!isset(
$this->m_mysqli_result))
     return
NULL;
  
  
/**
     * Do you know a generic way for PHP to retrieve a member
     * function instead of this ugly switch?
     */
  
switch ($i_varname) {
     case
'current_field': return $this->m_mysqli_result->current_field;
     case
'field_count':  return $this->m_mysqli_result->field_count;
     case
'lengths':      return $this->m_mysqli_result->lengths;
     case
'num_rows':      return $this->m_mysqli_result->num_rows;
     case
'type':          return $this->m_mysqli_result->type;
   }
  
   throw new
Exception('Trying to get non-existent mysqli property ' . $i_varname);
  }
 
 
// forward mysqli_result function calls
 
public function __call($i_funcname, $i_arguments) {
  
call_user_func_array(array(&$this->m_mysqli_result, $i_funcname), $i_arguments);
  }
}
?>

Example usage:

<?php
function some_query_func($i_query, $i_resultmode = MYSQLI_STORE_RESULT) {
 
$conn = getExistingMysqliObjectFromSomePlace();

 
$result = @$conn->query($i_query, $i_resultmode);
  if (
$result === FALSE || $result === NULL)
   throw new
Exception($conn->error);

  return
$result instanceof mysqli_result ? new MysqliResultWrapper($result) : $result;
}

$result = some_query_func($query);// unhandled Exception thrown on failure

if ($result->num_rows > 0)// calls __get()
 
while ($assoc = $result->fetch_assoc())// calls __call()
  
;// ...
?>
jon at fuck dot org
11-Jan-2006 10:39
better to wrap the result object in its own class, separate from the connection object wrapper: __destruct() can be used to automatically free the result set. this isn't a terribly new design pattern, either.

just be sure to destroy your result object before you make another query with the same DBI instance if you happen to change the flag to MYSQLI_USE_RESULT. actually, you'll find if you write the code with or without a wrapper class that it looks rather the same.

this code also allows you to track what kind of result set you've created by passing the flag around, although i don't think i'd ever need to use that myself.

class DBI
{
  function __construct()
  {
   $this->dbconn = new mysqli(...);
  }

  function query($sql, $flag = MYSQLI_STORE_RESULT)
  {
   $result = $this->dbconn->query($sql, $flag);
   return new RecordSet($result, $flag);
  }
};

class RecordSet
{
  function __construct($res, $flag = MYSQLI_STORE_RESULT)
  {
   $this->result = $res;
   $this->flag = $flag;
  }

  function __destruct()
  {
   $this->result->free();
  }
};
austregecilio at yahoo dot com dot br
16-Jun-2005 06:52
Using mysqli_free_result in a class, should be like this:

class db{
  .
  .
  .
  public function db_free_res($obj){
   mysqli_free_result($obj);
  }
  .
  .
  .
}

The code in .PHP file:

$obj_con=new db;
$res=$obj_con->db_query("select * from imgs");
$obj_con->db_free_res($res);

<mysqli_field_tellmysqli_get_client_info>
 Last updated: Tue, 15 Nov 2005