/** * Fetch resultset from a prepared statement * * @param mysli_stmt $stmtHandle * @param $filter $null callback or closure to filter a row * @return mixed */ public function fetchPreparedData(mysqli_stmt $stmt, ErrorStackInterface $errorStack, $filter = null) { $data = array(); $idx = 0; $isNext = false; do { switch ($stmt->fetch()) { case true: $row = array_combine($this->columnData['names'], $this->dereferenceColumnValues()); if (is_callable($filter)) { try { $row = call_user_func($filter, $row); } catch (Exception $e) { $errText = $e->getMessage() . " -({$idx})"; $errorStack->addError($errText, $e->getCode()); $row = false; } } $data[] = $row; $idx++; $isNext = true; break; case null: $isNext = false; break; case false: $error = $stmt->error . " -({$idx}) " . $stmt->sqlstate; $errorStack->addError($error, $stmt->errorno); return false; default: $msg = "unknown return value mysqli_stmt::fetch -({$idx})"; $errorStack->addError(500, $msg); return false; } } while ($isNext); $this->free(); return $data; }
/** * Fetch all the rows allowing access to each row with a callback or * closure * * @param int $type * @param mixed string | array | closure * @return array */ public function fetchAllData(ErrorStackInterface $errorStack, $type = MYSQLI_ASSOC, $filter = null) { if (!$this->isHandle()) { $errorStack->addError("Result handle is not available", 500); return false; } if (!$this->isValidType($type)) { $this->free(); $err = "fetchAllData failed: invalid result type -({$type})"; $errorStack->setError($msg, 500); return false; } /* * No need to go foward knowing the callback is faulty */ if (!is_callable($filter) && !empty($filter)) { $msg = 'fetchAllData failed: invalid callback'; $errorStack->seError($msg, 500); return false; } $idx = 0; $handle = $this->getHandle(); $data = array(); $error = array(); while ($row = $handle->fetch_array($type)) { if (!is_callable($filter)) { $data[] = $row; $idx++; continue; } try { $data[] = call_user_func($filter, $row); } catch (Exception $e) { $errText = $e->getMessage() . " -({$idx})"; $errorStack->addError($errText, $e->getCode()); $data[] = false; } $idx++; } $this->free(); return $data; }