Statement on glibc/iconv Vulnerability

Класс mysqli_result

(PHP 5, PHP 7, PHP 8)

Введение

Представляет результирующий набор, полученный из запроса в базу данных.

Обзор классов

class mysqli_result implements IteratorAggregate {
/* Свойства */
public readonly int $current_field;
public readonly int $field_count;
public readonly ?array $lengths;
public readonly int|string $num_rows;
public int $type;
/* Методы */
public __construct(mysqli $mysql, int $result_mode = MYSQLI_STORE_RESULT)
public data_seek(int $offset): bool
public fetch_all(int $mode = MYSQLI_NUM): array
public fetch_column(int $column = 0): null|int|float|string|false
public fetch_object(string $class = "stdClass", array $constructor_args = []): object|null|false
public field_seek(int $index): true
public free(): void
public close(): void
public free_result(): void
}

Свойства

type

Сохраняет буферизованный или небуферизованный результат в виде целого числа (int) (MYSQLI_STORE_RESULT или MYSQLI_USE_RESULT соответственно).

Список изменений

Версия Описание
8.0.0 Класс mysqli_result теперь реализует интерфейс IteratorAggregate. Ранее вместо него был реализован интерфейс Traversable.

Содержание

  • mysqli_result::__construct — Конструктор объекта mysqli_result
  • mysqli_result::$current_field — Получает смещение указателя по отношению к текущему полю
  • mysqli_result::data_seek — Перемещает указатель результата на выбранную строку
  • mysqli_result::fetch_all — Выбирает все строки из результирующего набора и помещает их в ассоциативный массив, обычный массив или в оба
  • mysqli_result::fetch_array — Выбирает следующую строку из набора результатов и помещает её в ассоциативный массив, обычный массив или в оба
  • mysqli_result::fetch_assoc — Выбирает следующую строку из набора результатов и помещает её в ассоциативный массив
  • mysqli_result::fetch_column — Получает один столбец из следующей строки набора результатов
  • mysqli_result::fetch_field_direct — Получает метаданные конкретного поля
  • mysqli_result::fetch_field — Возвращает следующее поле результирующего набора
  • mysqli_result::fetch_fields — Возвращает массив объектов, которые представляют поля результирующего набора
  • mysqli_result::fetch_object — Выбирает следующую строку из набора результатов в виде объекта
  • mysqli_result::fetch_row — Выбирает следующую строку из набора результатов и помещает её в обычный массив
  • mysqli_result::$field_count — Получает количество полей в наборе результатов
  • mysqli_result::field_seek — Устанавливает указатель результата на заданное смещение поля
  • mysqli_result::free — Освобождает память, занятую результатами запроса
  • mysqli_result::getIterator — Извлекает внешний итератор
  • mysqli_result::$lengths — Возвращает длины полей текущей строки результирующего набора
  • mysqli_result::$num_rows — Получает количество строк в наборе результатов
add a note

User Contributed Notes 4 notes

up
70
tuxedobob
11 years ago
Converting an old project from using the mysql extension to the mysqli extension, I found the most annoying change to be the lack of a corresponding mysql_result function in mysqli. While mysql_result is a generally terrible function, it was useful for fetching a single result field *value* from a result set (for example, if looking up a user's ID).

The behavior of mysql_result is approximated here, though you may want to name it something other than mysqli_result so as to avoid thinking it's an actual, built-in function.

<?php
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return
$datarow[$field];
}
?>

Implementing it via the OO interface is left as an exercise to the reader.
up
18
Cem Kayali / cemkayali(a)eticaret.com.tr
5 years ago
Switching from Php5 to Php7, especially if you have worked on an ongoing, long term project, it is unfortunate that there is no mysqli_result function.

So, this may be helpfull and you can call this function as you wish. I assume you do restricted search (searching for single row or few rows only).

function mysqli_result($search, $row, $field){
$i=0; while($results=mysqli_fetch_array($search)){
if ($i==$row){$result=$results[$field];}
$i++;}
return $result;}

Simply;

$search=mysqli_query($connection, "select name from table_name where id='7'");
$name=mysqli_result($search, 0, "id");

Best wishes,
up
14
Marc17
10 years ago
An "mysqli_result" function where $field can be like table_name.field_name with alias or not.
<?php
function mysqli_result($result,$row,$field=0) {
if (
$result===false) return false;
if (
$row>=mysqli_num_rows($result)) return false;
if (
is_string($field) && !(strpos($field,".")===false)) {
$t_field=explode(".",$field);
$field=-1;
$t_fields=mysqli_fetch_fields($result);
for (
$id=0;$id<mysqli_num_fields($result);$id++) {
if (
$t_fields[$id]->table==$t_field[0] && $t_fields[$id]->name==$t_field[1]) {
$field=$id;
break;
}
}
if (
$field==-1) return false;
}
mysqli_data_seek($result,$row);
$line=mysqli_fetch_array($result);
return isset(
$line[$field])?$line[$field]:false;
}
?>
up
1
blar at blar dot de
15 years ago
Extending the MySQLi_Result

<?php

class Database_MySQLi extends MySQLi
{
public function
query($query)
{
$this->real_query($query);
return new
Database_MySQLi_Result($this);
}
}

class
Database_MySQLi_Result extends MySQLi_Result
{
public function
fetch()
{
return
$this->fetch_assoc();
}

public function
fetchAll()
{
$rows = array();
while(
$row = $this->fetch())
{
$rows[] = $row;
}
return
$rows;
}
}

?>
To Top