Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue where columns with same name were causing bind to break, add... #3

Merged
merged 1 commit into from
Mar 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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