Wednesday, August 22, 2007

php 5 mysqli wrapper - data reader


// mysql data reader class
class MySqlDataReader{

//
// private members
//

private $Statement;
private $Results;
private $Bound;

//
// constructor
//

public function __construct($Statement = ''){

$this->Statement = $Statement;
$this->Bound = false;
}

//
// private methods
//

// bind output parameters
private function BindResults(){

if(count($this->Results)){

$this->Bound = true;
// $this->Statement->bind_result($this->Results[0], $this->Results[1], ...);
call_user_func_array(array($this->Statement, 'bind_result'), $this->Results);
}
}

//
// public methods
//

// helper method for adding bound results
public function AddResult(&$result){
$this->Results[] = &$result;
}

// read data from database
public function Read(){

// bind result parameters on first call to read
if(!$this->Bound)
$this->BindResults();

// fetch next row of data
if(is_object($this->Statement))
return $this->Statement->fetch();
}

// close the statement if it hasn't been closed already
public function Close(){

if(is_object($this->Statement))
$this->Statement->close();
}

}
?>

No comments: