PHP mysqli_result Class
The PHP mysqli_result class represents the result set obtained after running a query.
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/#propertiesIn 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_fieldGets 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_countNumber of fields in a result set.
Sintaxis / Sintax$mysqli_result->field_count;
$lengths
https://blastcoding.com/en/php-mysqli_result-class/#lengthsReturns 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_rowsNumber of rows returned in the result.
Sintaxis / Sintax$mysqli_result->num_rows;
$type
https://blastcoding.com/en/php-mysqli_result-class/#typeIndicates 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
$mysqli_result->type;
Methods
https://blastcoding.com/en/php-mysqli_result-class/#methodsIn 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/#__constructThe 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
.
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_seekThe 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.
pointer | name | surname | address |
---|---|---|---|
Marcelo | Kappa | Rotham 2312 | |
→ | Mariana | Suarez | Lomas 2782 |
Gerardo | Isil | Dime 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ónpublic mysqli_result::data_seek(int $offset): boolSintaxis / 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_allReturns all rows in the result as an associative, numeric, or both types of arrays, depending on the mode passed as a parameter.
Description / Descripciónpublic mysqli_result::fetch_all(int $mode = MYSQLI_NUM): arraySintaxis / 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_arrayReturns the result of the next row as an associative, numeric, or both types of array.
Description / Descripciónpublic mysqli_result::fetch_array(int $mode = MYSQLI_BOTH): array|null|falseSintaxis / 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_assocExtracts or returns the next row from a result set as an associative array.
Description / Descripciónpublic mysqli_result::fetch_assoc(): array|null|falseSintaxis / 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_columnRetrieves a column from the next row in the result set. See the following table, assuming our pointer is positioned as seen in the table.
nombre | apellido | direccion | |
Marcelo | Kappa | Rotham 2312 | |
→ | Mariana | Suarez | Lomas 2782 |
Gerardo | Isil | Dime 1782 |
If I call this method for column 2, $result->fetch_column(2)
I will get “Tell me 1782.
public mysqli_result::fetch_column(int $column = 0): null|int|float|string|falseSintaxis / 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_directGets the metadata of a single field as an object.
Attribute | Description |
---|---|
name | The name of the column |
orgname | Original column name if an alias was specified |
table | The name of the table this field belongs to (if not calculated) |
orgtable | Original table name if an alias was specified |
def | The default value for this field, represented as a string |
max_length | The maximum width of the field for the result set. |
length | The width of the field, as specified in the table definition. |
charsetnr | The character set number for the field. |
flags | An integer representing the bit-flags for the field. |
type | The data type used for this field |
decimals | The number of decimals used (for numeric fields) |
public mysqli_result::fetch_field_direct(int $index): object|falseSintaxis / 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_fieldReturns the information of the next field in the result set
Attribute | Description |
---|---|
name | The name of the column |
orgname | Original column name if an alias was specified |
table | The name of the table this field belongs to (if not calculated) |
orgtable | Original table name if an alias was specified |
def | The default value for this field, represented as a string |
max_length | The maximum width of the field for the result set. |
length | The width of the field, as specified in the table definition. |
charsetnr | The character set number for the field. |
flags | An integer representing the bit-flags for the field. |
type | The data type used for this field |
decimals | The number of decimals used (for numeric fields) |
public mysqli_result::fetch_field(): object|falseSintaxis / 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_fieldsReturns an array of objects with the metadata of the fields. The objects will have the following attributes or properties.
Attribute | Description |
---|---|
name | The name of the column |
orgname | Original column name if an alias was specified |
table | The name of the table this field belongs to (if not calculated) |
orgtable | Original table name if an alias was specified |
def | The default value for this field, represented as a string |
max_length | The maximum width of the field for the result set. |
length | The width of the field, as specified in the table definition. |
charsetnr | The character set number for the field. |
flags | An integer representing the bit-flags for the field. |
type | The data type used for this field |
decimals | The number of decimals used (for numeric fields) |
public mysqli_result::fetch_fields(): arraySintaxis / Sintax
$result->fetch_fields();
Returns
Array of objects
fetch_object
https://blastcoding.com/en/php-mysqli_result-class/#fetch_objectExtracts the next row from the result set and returns it as an object.
Description / Descripciónpublic mysqli_result::fetch_object(string $class = "stdClass", array $constructor_args = []): object|null|falseSintaxis / 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_rowExtracts the new row from the result set and returns an enumerated array.
Description / Descripciónpublic mysqli_result::fetch_row(): array|null|falseSintaxis / 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_seekSet result pointer to a specified field offset($index)
Description / Descripciónpublic mysqli_result::field_seek(int $index): boolSintaxis / 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/#freeFrees the memory associated with the result.
Description / Descripciónpublic mysqli_result::free(): voidDescription / Descripción
public mysqli_result::close(): voidDescription / Descripción
public mysqli_result::free_result(): voidSintaxis / Sintax
$result->close();
getIterator
https://blastcoding.com/en/php-mysqli_result-class/#getIteratorReturns an external iterator
Description / Descripciónpublic mysqli_result::getIterator(): IteratorSintaxis / Sintax
$iterator = $result->getIterator();
Return
Returns an iterator for whatever you need to do with the result.