/** * @param Connection $connection * @param null|\mysqli_result $result */ public function __construct(Connection $connection, $result = null) { $this->connection = $connection; if ($result instanceof \mysqli_result) { $this->result = $result; $this->num_rows = $this->result->num_rows; $this->fields = $this->result->fetch_fields(); } else { $this->affected_rows = $this->getMysqliConnection()->affected_rows; } }
/** * Returns an array of fields according to columns in the result. * * @return \Bitrix\Main\Entity\ScalarField[] */ public function getFields() { if ($this->resultFields == null) { $this->resultFields = array(); if (is_object($this->resource)) { $fields = $this->resource->fetch_fields(); if ($fields && $this->connection) { $helper = $this->connection->getSqlHelper(); foreach ($fields as $field) { $this->resultFields[$field->name] = $helper->getFieldByColumnType($field->name, $field->type); } } } } return $this->resultFields; }
/** * @param mysqli_result $mysqli_result * @param bool $single * @return array|bool|null */ public function fetch($mysqli_result, $single) { $this->_('fields', array()); foreach ($mysqli_result->fetch_fields() as $field) { $this->_('fields')[$field->name] = $this->rules[$this->types[$field->type]]; } switch (true) { case !$mysqli_result: return null; case $single && $mysqli_result->num_rows == 0: $result = false; break; case $single: $result = $this->cast($mysqli_result->fetch_assoc()); break; case $mysqli_result->num_rows == 0: $result = array(); break; default: $result = array(); while ($row = $mysqli_result->fetch_assoc()) { $result[] = $this->cast($row); } } $mysqli_result->free(); return $result; }
/** * Return properties taken from result fields as array(props, default props) * * @return array */ protected function fetchFieldProperties() { foreach ($this->native->fetch_fields() as $i => $field) { $props[] = $this->convertFieldProperties($field, $i); } return $props; }
/** * @param \mysqli_result $resource * @param string $column * * @return mixed[] */ protected function fetchResource($resource, $column) { $fields = $resource->fetch_fields(); if (count($fields) == 0) { return []; } $result = $resource->fetch_all(MYSQLI_ASSOC); if (!is_array($result)) { return []; } $resource->free(); $this->fixTypes($result, $fields, $column); return $result; }
/** * Purpose: Display the results of a database query in an HTML table * @param mysqli_result $qryResults Results of a previous database query */ public static function displayRecords($qryResults) { // Display the opening table tag echo "<table>\n"; // Display a table row opening tag echo " <tr>"; // LOOP for all query result columns foreach ($qryResults->fetch_fields() as $fieldInfo) { // Display the column name within a table header tag echo "<th>{$fieldInfo->name}</th>"; } // Display a table row closing tag echo "</tr>\n"; // LOOP for all the query rows returned while ($row = $qryResults->fetch_row()) { // Display a table row opening tag echo " <tr>"; // LOOP for all the query result columns for ($i = 0; $i < $qryResults->field_count; $i++) { // Display the value of this query result row and column echo "<td>" . htmlspecialchars($row[$i]) . "</td>"; // $row[$i] = htmlspecialchars( $row[$i] ); // echo "<td>{$row[$i]}</td>"; } // Display a table row closing tag echo " </tr>\n"; } // Display the closing table tag echo "</table>\n"; }