mysqli - How to free memory in php class method -
i present part of mine class works correctly.
want advice in:
how release resources $result->free()
method in function , place them in code.
is useful write @ end of function, when return value before? when have placed before return operator, function doesn't work.
thanks in advance!
require_once 'ggc_config.php'; class ggc { public static function executequery($querystring, &$id){ $mysqli = new mysqli(_ggc_host_ , _ggc_user_ , _ggc_password_ , _ggc_db_); if (mysqli_connect_errno()) { echo("connect failed: ". mysqli_connect_error()); return 0; }//if $mysqli->set_charset("utf8"); $result=$mysqli->query($querystring); if ($result===true){ $id=$mysqli->insert_id; return 1; }//if else{ return 0; }//else $result->free(); $mysqli->close(); }//executequeryi public static function getresults($querystring){ $mysqli = new mysqli(_ggc_host_ , _ggc_user_ , _ggc_password_ , _ggc_db_); if (mysqli_connect_errno()){ die("connect failed: ". mysqli_connect_error()); }//if $mysqli->set_charset("utf8"); if ($result = $mysqli->query($querystring)){ while ($row = $result->fetch_row()) { ($i=0; $i<$mysqli->field_count; $i++){ $a[$i][]= $row[$i]; }//for }//while }//if return $a; $result->free(); $mysqli->close(); }//function }//end of class
due advicies have changet mine code this:
require_once 'ggc_config.php';
class ggc { public static function executequery($querystring, &$id){ $mysqli = new mysqli(_ggc_host_ , _ggc_user_ , _ggc_password_ , _ggc_db_); if (mysqli_connect_errno()) { echo("connect failed: ". mysqli_connect_error()); unset($mysqli); return 0; }//if $mysqli->set_charset("utf8"); $result=$mysqli->query($querystring); if ($result===true){ $id=$mysqli->insert_id; unset($result); unset($mysqli); return 1; }//if else{ unset($result); unset($mysqli); return 0; }//else }//executequery public static function getresults($querystring){ $mysqli = new mysqli(_ggc_host_ , _ggc_user_ , _ggc_password_ , _ggc_db_); if (mysqli_connect_errno()){ die("connect failed: ". mysqli_connect_error()); }//if $mysqli->set_charset("utf8"); if ($result = $mysqli->query($querystring)){ while ($row = $result->fetch_row()) { ($i=0; $i<$mysqli->field_count; $i++){ $a[$i][]= $row[$i]; }//for }//while }//if unset($result); unset($mysqli); return $a; }//function }//end of class
update:
of course, way wrote somehow wrong. can not after return clause because end function execution. you'll need free result before returning.
the way wrote it, with:
$result->free(); $mysqli->close();
would work place return clause after that.
$result->free(); $mysqli->close(); return $a;
Comments
Post a Comment