Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished |not checked

PHP mysqli_result Class

The PHP mysqli_result class represents the result set obtained after running a query.

contenido
Sinopsis
class mysqli_result implements IteratorAggregate {
/* Properties */
public readonly int $current_field;
public readonly int $field_count;
public readonly ?array $lengths;
public readonly int|string $num_rows;
public int $type;
/* Methods */
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_array(int $mode = MYSQLI_BOTH): array|null|false
public fetch_assoc(): array|null|false
public fetch_column(int $column = 0): null|int|float|string|false
public fetch_field_direct(int $index): object|false
public fetch_field(): object|false
public fetch_fields(): array
public fetch_object(string $class = "stdClass", array $constructor_args = []): object|null|false
public fetch_row(): array|null|false
public field_seek(int $index): bool
public free(): void
public close(): void
public free_result(): void
public getIterator(): Iterator
}

We can obtain mysqli_result objects in several ways, both from a mysqli object and from a mysqli_stmt object.

From mysqli, we have the following methods:

  • mysqli reap_async_query
  • mysqli store_result
  • mysqli use_result

From mysqli_stmt, we have:

  • mysqli_stmt get_result
  • mysqli_stmt result_metadata

Lastly, we could use its constructor.

Before diving into the properties and methods, we should remember that a query or prepared statement will have already been executed before using these properties or methods.

Properties

https://blastcoding.com/en/php-mysqli_result-class/#properties

In the following properties, you will see the variable $mysqli_result in the syntax, which will be holding a mysqli_result object.

$current_field

https://blastcoding.com/en/php-mysqli_result-class/#current_field

Gets the position of the current field in a result set pointer. You will need to use fetch_field to access this property.

Sintaxis / Sintax
$mysqli_result->current_field;

$field_count

https://blastcoding.com/en/php-mysqli_result-class/#field_count

Number of fields in a result set.

Sintaxis / Sintax
$mysqli_result->field_count;

$lengths

https://blastcoding.com/en/php-mysqli_result-class/#lengths

Returns the length of the columns of the current row in the result set as an array; it may return null or false in case of an error.

Sintaxis / Sintax
$mysqli_result->lengths;

$num_rows

https://blastcoding.com/en/php-mysqli_result-class/#num_rows

Number of rows returned in the result.

Sintaxis / Sintax
$mysqli_result->num_rows;

$type

https://blastcoding.com/en/php-mysqli_result-class/#type

Indicates whether the result is buffered or unbuffered as an integer (int), with these values represented by the constants MYSQLI_STORE_RESULT and MYSQLI_USE_RESULT, respectively

Sintaxis / Sintax
$mysqli_result->type;

Methods

https://blastcoding.com/en/php-mysqli_result-class/#methods

In the following syntax examples, we will use the variable $result to hold a mysqli_result object. Keep in mind that to obtain a mysqli_result, it should be created in one of the ways mentioned earlier or with the constructor.

__construct

https://blastcoding.com/en/php-mysqli_result-class/#__construct

The constructor of the mysqli_result class is responsible for building the object. The constructor can be used after using the real_query method of mysqli.

Description / Descripción
public mysqli_result::__construct(mysqli $mysqli, int $result_mode = MYSQLI_STORE_RESULT)
Sintaxis / Sintax
$result = new mysqli_result($mysqli,$reslt_mode)

data_seek

https://blastcoding.com/en/php-mysqli_result-class/#data_seek

The data_seek method allows you to reposition the internal pointer of a MySQL result set to a specific row. This is useful when you want to access rows in a non-sequential order or when you need to revisit a previous row.

For example, take a look at the following table, our arrow is where we are pointing at, or better say where we want to point at. Let’s indicate in our $offset parameter where we want to point at.

pointernamesurnameaddress
MarceloKappaRotham 2312
MarianaSuarezLomas 2782
GerardoIsilDime 1782

In this context, if we want to point at the row where Mariana is $result->data_seek(1) this is cause the offset starts at 0 and finish in length-1 like an array.

If we look at the table, the indexes are 0, 1 and 2 and the length is 3.

Description / Descripción
public mysqli_result::data_seek(int $offset): bool
Sintaxis / Sintax
$result->data_seek($offset);

Parameter

$offset – Row number; this number must be between 0 and the length of rows -1.

Returns

true at success or false if not.

fetch_all

https://blastcoding.com/en/php-mysqli_result-class/#fetch_all

Returns all rows in the result as an associative, numeric, or both types of arrays, depending on the mode passed as a parameter.

Description / Descripción
public mysqli_result::fetch_all(int $mode = MYSQLI_NUM): array
Sintaxis / Sintax
$result->fetch_all($mode);

Parameter

$mode – mode can be one of the following constants: MYSQLI_NUM (default), MYSQLI_ASSOC, and MYSQLI_BOTH.

Returns

This method will return an array, which will depend on the specified $mode parameter

fetch_array

https://blastcoding.com/en/php-mysqli_result-class/#fetch_array

Returns the result of the next row as an associative, numeric, or both types of array.

Description / Descripción
public mysqli_result::fetch_array(int $mode = MYSQLI_BOTH): array|null|false
Sintaxis / Sintax
$result->fetch_array($mode);

Parameter

$mode -mode can be one of the following constants: MYSQLI_NUM, MYSQLI_ASSOC, and MYSQLI_BOTH (default).

Returns

array with the values of the row, null if there are no more rows in the result set, and false in case of an error.

fetch_assoc

https://blastcoding.com/en/php-mysqli_result-class/#fetch_assoc

Extracts or returns the next row from a result set as an associative array.

Description / Descripción
public mysqli_result::fetch_assoc(): array|null|false
Sintaxis / Sintax
$result->fetch_assoc();

Returns

Returns an associative array, null if there are no more rows, and false in case of failure.

fetch_column

https://blastcoding.com/en/php-mysqli_result-class/#fetch_column

Retrieves a column from the next row in the result set. See the following table, assuming our pointer is positioned as seen in the table.

nombreapellidodireccion
MarceloKappaRotham 2312
MarianaSuarezLomas 2782
GerardoIsilDime 1782

If I call this method for column 2, $result->fetch_column(2) I will get “Tell me 1782.

Description / Descripción
public mysqli_result::fetch_column(int $column = 0): null|int|float|string|false
Sintaxis / Sintax
$result->fetch_column($column);

Parameter

$column – Column to be retrieved; note that column numbering starts from 0.

Returns

This method has various possible returns, as the value can be anything present in the table – mixed, or false if there are no more rows.

fetch_field_direct

https://blastcoding.com/en/php-mysqli_result-class/#fetch_field_direct

Gets the metadata of a single field as an object.

AttributeDescription
nameThe name of the column
orgnameOriginal column name if an alias was specified
tableThe name of the table this field belongs to (if not calculated)
orgtableOriginal table name if an alias was specified
defThe default value for this field, represented as a string
max_lengthThe maximum width of the field for the result set.
lengthThe width of the field, as specified in the table definition.
charsetnrThe character set number for the field.
flags An integer representing the bit-flags for the field.
typeThe data type used for this field
decimalsThe number of decimals used (for numeric fields)
Description / Descripción
public mysqli_result::fetch_field_direct(int $index): object|false
Sintaxis / Sintax
$camp_info = $result->fetch_field_direct($index);

As you can see in the syntax, $camp_info will hold our object. With this variable, we can retrieve the values of the different attributes shown in the table above. For example, $camp_info->name will get the column name, $camp_info->table will get the name of the table to which the field belongs, and so on.

Parameter

$index(field number) – its value varies between 0 and the number of fields -1.

Returns

Returns an object or false if there is no information for the field number $index.

fetch_field

https://blastcoding.com/en/php-mysqli_result-class/#fetch_field

Returns the information of the next field in the result set

AttributeDescription
nameThe name of the column
orgnameOriginal column name if an alias was specified
tableThe name of the table this field belongs to (if not calculated)
orgtableOriginal table name if an alias was specified
defThe default value for this field, represented as a string
max_lengthThe maximum width of the field for the result set.
lengthThe width of the field, as specified in the table definition.
charsetnrThe character set number for the field.
flags An integer representing the bit-flags for the field.
typeThe data type used for this field
decimalsThe number of decimals used (for numeric fields)
Description / Descripción
public mysqli_result::fetch_field(): object|false
Sintaxis / Sintax
$camp_info = $result->fetch_field();

Returns

Returns an object or false if there is no information for the field.

fetch_fields

https://blastcoding.com/en/php-mysqli_result-class/#fetch_fields

Returns an array of objects with the metadata of the fields. The objects will have the following attributes or properties.

AttributeDescription
nameThe name of the column
orgnameOriginal column name if an alias was specified
tableThe name of the table this field belongs to (if not calculated)
orgtableOriginal table name if an alias was specified
defThe default value for this field, represented as a string
max_lengthThe maximum width of the field for the result set.
lengthThe width of the field, as specified in the table definition.
charsetnrThe character set number for the field.
flags An integer representing the bit-flags for the field.
typeThe data type used for this field
decimalsThe number of decimals used (for numeric fields)
Description / Descripción
public mysqli_result::fetch_fields(): array
Sintaxis / Sintax
$result->fetch_fields();

Returns

Array of objects

fetch_object

https://blastcoding.com/en/php-mysqli_result-class/#fetch_object

Extracts the next row from the result set and returns it as an object.

Description / Descripción
public mysqli_result::fetch_object(string $class = "stdClass", array $constructor_args = []): object|null|false
Sintaxis / Sintax
$objRow = $result->fetch_object($class,$constructor_args);

Parameter

$class -Name of the class to be instantiated, usually this class is a stdClass. The stdClass class represents an empty object.

$constructor_args – in case the class needs arguments

Returns

Returns an object if it could obtain the values, null if there are no more rows, and false in case of failure.

fetch_row

https://blastcoding.com/en/php-mysqli_result-class/#fetch_row

Extracts the new row from the result set and returns an enumerated array.

Description / Descripción
public mysqli_result::fetch_row(): array|null|false
Sintaxis / Sintax
$e_row = $result->fetch_row();

Returns

Returns an array if everything went well, null if there are no more rows, and false in case of failure.

field_seek

https://blastcoding.com/en/php-mysqli_result-class/#field_seek

Set result pointer to a specified field offset($index)

Description / Descripción
public mysqli_result::field_seek(int $index): bool
Sintaxis / Sintax
$result->field_seek($index);

Parameter

$index – Field index, which can be a value between 0 and the number of fields – 1.

Returns

Returns true if it can point to the specified field or false if there was a failure.

free / close / free_result

https://blastcoding.com/en/php-mysqli_result-class/#free

Frees the memory associated with the result.

Description / Descripción
public mysqli_result::free(): void
Description / Descripción
public mysqli_result::close(): void
Description / Descripción
public mysqli_result::free_result(): void
Sintaxis / Sintax
$result->close();

getIterator

https://blastcoding.com/en/php-mysqli_result-class/#getIterator

Returns an external iterator

Description / Descripción
public mysqli_result::getIterator(): Iterator
Sintaxis / Sintax
$iterator = $result->getIterator();

Return

Returns an iterator for whatever you need to do with the result.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments