Skip to content

Commit

Permalink
Merge pull request #3 from ddmills/results-update
Browse files Browse the repository at this point in the history
fixed issue where columns with same name were causing bind to break, add...
  • Loading branch information
ddmills committed Mar 18, 2015
2 parents a4f7b65 + 6c8dede commit c0d262f
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions mywrap_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ class mywrap_result {
* Constructor -
* Creates a result object from a prepared statement
*/
public function __construct($statement) {
public function __construct($statement) {
$this->statement = $statement;
$this->bound_variables = array();
$this->bound_variables = array();
$this->results = array();
$this->columns = array();
$meta = $this->statement->result_metadata();
if ($meta) {
while ($column = $meta->fetch_field()) {
$this->bound_variables[$column->name] =& $this->results[$column->name];
if (isset($this->columns[$column->name])) {
$this->columns[$column->name . '_copy'] = $column;
$this->bound_variables[$column->name . '_copy'] =& $this->results[$column->name . '_copy'];
} else {
$this->columns[$column->name] = $column;
$this->bound_variables[$column->name] =& $this->results[$column->name];
}
}
call_user_func_array(array($this->statement, 'bind_result'), $this->bound_variables);
$meta->close();
Expand All @@ -43,17 +50,17 @@ public function get_statement() {
}

/**
* Fetch result while it can, returns false when finished
* count the number of rows in the result
*/
public function fetch() {
return $this->statement->fetch() ? $this->results : false;
public function count() {
return $this->statement->num_rows();
}

/**
* count the number of rows in the result
* get the column names for this result
*/
public function rows() {
return $this->statement->num_rows();
public function columns() {
return $this->columns;
}

/**
Expand All @@ -63,10 +70,17 @@ public function affected_rows() {
return $this->statement->affected_rows;
}

/**
* Fetch result while it can, returns false when finished
*/
public function fetch() {
return $this->statement->fetch() ? $this->results : false;
}

/**
* Fetch a result row as an associative array, returns false when finished
*/
public function fetch_array() {
public function fetch_array() {
$results = $this->fetch();
if ($results) {
$row = array();
Expand All @@ -76,7 +90,22 @@ public function fetch_array() {
return $row;
}
return false;
}
}

/**
* Fetch all results in a multi-level array
*/
public function fetch_all_array() {
/* return cached version if exists */
if (isset($this->cached)) return $this->cached;

$results = array();
while ($result = $this->fetch_array()) {
array_push($results, $result);
}
$this->cached = $results;
return $results;
}

/**
* retrieve the last id that was inserted
Expand Down

0 comments on commit c0d262f

Please sign in to comment.