Spec-Zone .ru
спецификации, руководства, описания, API

22.9.3.11. The mysqli_result class (mysqli_result)

Copyright 1997-2012 the PHP Documentation Group.

Represents the result set obtained from a query against the database.

Changelog

Table 22.55. Changelog

Version Description
5.4.0 Iterator support was added, as mysqli_result now implementsTraversable.

 mysqli_result {
mysqli_result , TraversableProperties int mysqli_result->current_field ;
int mysqli_result->field_count ;
array mysqli_result->lengths ;
int mysqli_result->num_rows ;
Methods bool mysqli_result::data_seek(int offset);
mixed mysqli_result::fetch_all(int resulttype= =MYSQLI_NUM);
mixed mysqli_result::fetch_array(int resulttype= =MYSQLI_BOTH);
array mysqli_result::fetch_assoc();
object mysqli_result::fetch_field_direct(int fieldnr);
object mysqli_result::fetch_field();
array mysqli_result::fetch_fields();
object mysqli_result::fetch_object(string class_name,
array params);

mixed mysqli_result::fetch_row();
bool mysqli_result::field_seek(int fieldnr);
void mysqli_result::free();
}

22.9.3.11.1. mysqli_result::$current_field,mysqli_field_tell

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::$current_field

    mysqli_field_tell

    Get current field offset of a result pointer

Description

Object oriented style

int mysqli_result->current_field ;

Procedural style

int mysqli_field_tell(mysqli_result result);

Returns the position of the field cursor used for the last mysqli_fetch_field call. This value can be used as an argument to mysqli_field_seek.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

Returns current offset of field cursor.

Examples

Example 22.182. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = $mysqli->query($query)) {    /* Get field information for all columns */    while ($finfo = $result->fetch_field()) {        /* get fieldpointer offset */        $currentfield = $result->current_field;        printf("Column %d:\n", $currentfield);        printf("Name:     %s\n", $finfo->name);        printf("Table:    %s\n", $finfo->table);        printf("max. Len: %d\n", $finfo->max_length);        printf("Flags:    %d\n", $finfo->flags);        printf("Type:     %d\n\n", $finfo->type);    }    $result->close();}/* close connection */$mysqli->close();?>

Example 22.183. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = mysqli_query($link, $query)) {    /* Get field information for all fields */    while ($finfo = mysqli_fetch_field($result)) {        /* get fieldpointer offset */        $currentfield = mysqli_field_tell($result);        printf("Column %d:\n", $currentfield);        printf("Name:     %s\n", $finfo->name);        printf("Table:    %s\n", $finfo->table);        printf("max. Len: %d\n", $finfo->max_length);        printf("Flags:    %d\n", $finfo->flags);        printf("Type:     %d\n\n", $finfo->type);    }    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Column 1:Name:     NameTable:    Countrymax. Len: 11Flags:    1Type:     254Column 2:Name:     SurfaceAreaTable:    Countrymax. Len: 10Flags:    32769Type:     4

See Also

mysqli_fetch_field
mysqli_field_seek

22.9.3.11.2. mysqli_result::data_seek,mysqli_data_seek

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::data_seek

    mysqli_data_seek

    Adjusts the result pointer to an arbitrary row in the result

Description

Object oriented style

bool mysqli_result::data_seek(int offset);

Procedural style

bool mysqli_data_seek(mysqli_result result,
int offset);

The mysqli_data_seek function seeks to an arbitrary result pointer specified by the offset in the result set.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

offset

The field offset. Must be between zero and the total number of rows minus one (0..mysqli_num_rows - 1).

Return Values

Returns TRUE on success or FALSE on failure.

Notes

Note

This function can only be used with buffered results attained from the use of the mysqli_store_result or mysqli_query functions.

Examples

Example 22.184. Object oriented style

<?php/* Open a connection */$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER BY Name";if ($result = $mysqli->query( $query)) {    /* seek to row no. 400 */    $result->data_seek(399);    /* fetch row */    $row = $result->fetch_row();    printf ("City: %s  Countrycode: %s\n", $row[0], $row[1]);    /* free result set*/    $result->close();}/* close connection */$mysqli->close();?>

Example 22.185. Procedural style

<?php/* Open a connection */$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (!$link) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER BY Name";if ($result = mysqli_query($link, $query)) {    /* seek to row no. 400 */    mysqli_data_seek($result, 399);    /* fetch row */    $row = mysqli_fetch_row($result);    printf ("City: %s  Countrycode: %s\n", $row[0], $row[1]);    /* free result set*/    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

City: Benin City  Countrycode: NGA

See Also

mysqli_store_result
mysqli_fetch_row
mysqli_fetch_array
mysqli_fetch_assoc
mysqli_fetch_object
mysqli_query
mysqli_num_rows

22.9.3.11.3. mysqli_result::fetch_all,mysqli_fetch_all

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_all

    mysqli_fetch_all

    Fetches all result rows as an associative array, a numeric array, or both

Description

Object oriented style

mixed mysqli_result::fetch_all(int resulttype= =MYSQLI_NUM);

Procedural style

mixed mysqli_fetch_all(mysqli_result result,
int resulttype= =MYSQLI_NUM);

mysqli_fetch_all fetches all result rows and returns the result set as an associative array, a numeric array, or both.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

resulttype

This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC , MYSQLI_NUM , or MYSQLI_BOTH .

Return Values

Returns an array of associative or numeric arrays holding result rows.

MySQL Native Driver Only

Available only with mysqlnd.

As mysqli_fetch_all returns all the rows as an array in a single step, it may consume more memory than some similar functions such as mysqli_fetch_array, which only returns one row at a time from the result set. Further, if you need to iterate over the result set, you will need a looping construct that will further impact performance. For these reasons mysqli_fetch_all should only be used in those situations where the fetched result set will be sent to another layer for processing.

See Also

mysqli_fetch_array
mysqli_query

22.9.3.11.4. mysqli_result::fetch_array,mysqli_fetch_array

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_array

    mysqli_fetch_array

    Fetch a result row as an associative, a numeric array, or both

Description

Object oriented style

mixed mysqli_result::fetch_array(int resulttype= =MYSQLI_BOTH);

Procedural style

mixed mysqli_fetch_array(mysqli_result result,
int resulttype= =MYSQLI_BOTH);

Returns an array that corresponds to the fetched row or NULL if there are no more rows for the resultset represented by the result parameter.

mysqli_fetch_array is an extended version of the mysqli_fetch_row function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array function can also store the data in associative indices, using the field names of the result set as keys.

Note

Field names returned by this functionare case-sensitive.

Note

This function sets NULL fields tothe PHP NULL value.

If two or more columns of the result have the same field names, the last column will take precedence and overwrite the earlier data. In order to access multiple columns with the same name, the numerically indexed version of the row must be used.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

resulttype

This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC , MYSQLI_NUM , or MYSQLI_BOTH .

By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc, while MYSQLI_NUM will behave identically to the mysqli_fetch_row function. The final option MYSQLI_BOTH will create a single array with the attributes of both.

Return Values

Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.

Examples

Example 22.186. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";$result = $mysqli->query($query);/* numeric array */$row = $result->fetch_array(MYSQLI_NUM);printf ("%s (%s)\n", $row[0], $row[1]);/* associative array */$row = $result->fetch_array(MYSQLI_ASSOC);printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);/* associative and numeric array */$row = $result->fetch_array(MYSQLI_BOTH);printf ("%s (%s)\n", $row[0], $row["CountryCode"]);/* free result set */$result->free();/* close connection */$mysqli->close();?>

Example 22.187. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";$result = mysqli_query($link, $query);/* numeric array */$row = mysqli_fetch_array($result, MYSQLI_NUM);printf ("%s (%s)\n", $row[0], $row[1]);/* associative array */$row = mysqli_fetch_array($result, MYSQLI_ASSOC);printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);/* associative and numeric array */$row = mysqli_fetch_array($result, MYSQLI_BOTH);printf ("%s (%s)\n", $row[0], $row["CountryCode"]);/* free result set */mysqli_free_result($result);/* close connection */mysqli_close($link);?>   

The above examples will output:

Kabul (AFG)Qandahar (AFG)Herat (AFG)

See Also

mysqli_fetch_assoc
mysqli_fetch_row
mysqli_fetch_object
mysqli_query
mysqli_data_seek

22.9.3.11.5. mysqli_result::fetch_assoc,mysqli_fetch_assoc

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_assoc

    mysqli_fetch_assoc

    Fetch a result row as an associative array

Description

Object oriented style

array mysqli_result::fetch_assoc();

Procedural style

array mysqli_fetch_assoc(mysqli_result result);

Returns an associative array that corresponds to the fetched row or NULL if there are no more rows.

Note

Field names returned by this functionare case-sensitive.

Note

This function sets NULL fields tothe PHP NULL value.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysqli_fetch_row or add alias names.

Examples

Example 22.188. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = $mysqli->query($query)) {    /* fetch associative array */    while ($row = $result->fetch_assoc()) {        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);    }    /* free result set */    $result->free();}/* close connection */$mysqli->close();?>

Example 22.189. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = mysqli_query($link, $query)) {    /* fetch associative array */    while ($row = mysqli_fetch_assoc($result)) {        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);    }    /* free result set */    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Pueblo (USA)Arvada (USA)Cape Coral (USA)Green Bay (USA)Santa Clara (USA)

Example 22.190. A mysqli_result example comparingiterator usage

<?php$c = mysqli_connect('127.0.0.1','user', 'pass');// Using iterators (support was added with PHP 5.4)foreach ( $c->query('SELECT user,host FROM mysql.user') as $row ) {    printf("'%s'@'%s'\n", $row['user'], $row['host']);}echo "\n==================\n";// Not using iterators$result = $c->query('SELECT user,host FROM mysql.user');while ($row = $result->fetch_assoc()) {    printf("'%s'@'%s'\n", $row['user'], $row['host']);}?>   

The above example will output something similar to:

'root'@'192.168.1.1''root'@'127.0.0.1''dude'@'localhost''lebowski'@'localhost'=================='root'@'192.168.1.1''root'@'127.0.0.1''dude'@'localhost''lebowski'@'localhost'

See Also

mysqli_fetch_array
mysqli_fetch_row
mysqli_fetch_object
mysqli_query
mysqli_data_seek

22.9.3.11.6. mysqli_result::fetch_field_direct,mysqli_fetch_field_direct

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_field_direct

    mysqli_fetch_field_direct

    Fetch meta-data for a single field

Description

Object oriented style

object mysqli_result::fetch_field_direct(int fieldnr);

Procedural style

object mysqli_fetch_field_direct(mysqli_result result,
int fieldnr);

Returns an object which contains field definition information from the specified result set.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

fieldnr

The field number. This value must be in the range from 0 to number of fields - 1.

Return Values

Returns an object which contains field definition information or FALSE if no field information for specified fieldnr is available.

Table 22.56. Object attributes

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 integer fields)

Examples

Example 22.191. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Name LIMIT 5";if ($result = $mysqli->query($query)) {    /* Get field information for column 'SurfaceArea' */    $finfo = $result->fetch_field_direct(1);    printf("Name:     %s\n", $finfo->name);    printf("Table:    %s\n", $finfo->table);    printf("max. Len: %d\n", $finfo->max_length);    printf("Flags:    %d\n", $finfo->flags);    printf("Type:     %d\n", $finfo->type);    $result->close();}/* close connection */$mysqli->close();?>

Example 22.192. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Name LIMIT 5";if ($result = mysqli_query($link, $query)) {    /* Get field information for column 'SurfaceArea' */    $finfo = mysqli_fetch_field_direct($result, 1);    printf("Name:     %s\n", $finfo->name);    printf("Table:    %s\n", $finfo->table);    printf("max. Len: %d\n", $finfo->max_length);    printf("Flags:    %d\n", $finfo->flags);    printf("Type:     %d\n", $finfo->type);    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Name:     SurfaceAreaTable:    Countrymax. Len: 10Flags:    32769Type:     4

See Also

mysqli_num_fields
mysqli_fetch_field
mysqli_fetch_fields

22.9.3.11.7. mysqli_result::fetch_field,mysqli_fetch_field

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_field

    mysqli_fetch_field

    Returns the next field in the result set

Description

Object oriented style

object mysqli_result::fetch_field();

Procedural style

object mysqli_fetch_field(mysqli_result result);

Returns the definition of one column of a result set as an object. Call this function repeatedly to retrieve information about all columns in the result set.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

Returns an object which contains field definition information or FALSE if no field information is available.

Table 22.57. Object properties

Property 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 Reserved for default value, currently always ""
db Database (since PHP 5.3.6)
catalog The catalog name, always "def" (since PHP 5.3.6)
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 integer fields)

Examples

Example 22.193. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = $mysqli->query($query)) {    /* Get field information for all columns */    while ($finfo = $result->fetch_field()) {        printf("Name:     %s\n", $finfo->name);        printf("Table:    %s\n", $finfo->table);        printf("max. Len: %d\n", $finfo->max_length);        printf("Flags:    %d\n", $finfo->flags);        printf("Type:     %d\n\n", $finfo->type);    }    $result->close();}/* close connection */$mysqli->close();?>

Example 22.194. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = mysqli_query($link, $query)) {    /* Get field information for all fields */    while ($finfo = mysqli_fetch_field($result)) {        printf("Name:     %s\n", $finfo->name);        printf("Table:    %s\n", $finfo->table);        printf("max. Len: %d\n", $finfo->max_length);        printf("Flags:    %d\n", $finfo->flags);        printf("Type:     %d\n\n", $finfo->type);    }    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Name:     NameTable:    Countrymax. Len: 11Flags:    1Type:     254Name:     SurfaceAreaTable:    Countrymax. Len: 10Flags:    32769Type:     4

See Also

mysqli_num_fields
mysqli_fetch_field_direct
mysqli_fetch_fields
mysqli_field_seek

22.9.3.11.8. mysqli_result::fetch_fields,mysqli_fetch_fields

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_fields

    mysqli_fetch_fields

    Returns an array of objects representing the fields in a result set

Description

Object oriented style

array mysqli_result::fetch_fields();

Procedural style

array mysqli_fetch_fields(mysqli_result result);

This function serves an identical purpose to the mysqli_fetch_field function with the single difference that, instead of returning one object at a time for each field, the columns are returned as an array of objects.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

Returns an array of objects which contains field definition information or FALSE if no field information is available.

Table 22.58. Object properties

Property 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
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 integer fields)

Examples

Example 22.195. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = $mysqli->query($query)) {    /* Get field information for all columns */    $finfo = $result->fetch_fields();    foreach ($finfo as $val) {        printf("Name:     %s\n", $val->name);        printf("Table:    %s\n", $val->table);        printf("max. Len: %d\n", $val->max_length);        printf("Flags:    %d\n", $val->flags);        printf("Type:     %d\n\n", $val->type);    }    $result->close();}/* close connection */$mysqli->close();?>

Example 22.196. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = mysqli_query($link, $query)) {    /* Get field information for all columns */    $finfo = mysqli_fetch_fields($result);    foreach ($finfo as $val) {        printf("Name:     %s\n", $val->name);        printf("Table:    %s\n", $val->table);        printf("max. Len: %d\n", $val->max_length);        printf("Flags:    %d\n", $val->flags);        printf("Type:     %d\n\n", $val->type);    }    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Name:     NameTable:    Countrymax. Len: 11Flags:    1Type:     254Name:     SurfaceAreaTable:    Countrymax. Len: 10Flags:    32769Type:     4

See Also

mysqli_num_fields
mysqli_fetch_field_direct
mysqli_fetch_field

22.9.3.11.9. mysqli_result::fetch_object,mysqli_fetch_object

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_object

    mysqli_fetch_object

    Returns the current row of a result set as an object

Description

Object oriented style

object mysqli_result::fetch_object(string class_name,
array params);

Procedural style

object mysqli_fetch_object(mysqli_result result,
string class_name,
array params);

The mysqli_fetch_object will return the current row result set as an object where the attributes of the object represent the names of the fields found within the result set.

Note that mysqli_fetch_object sets the properties of the object before calling the object constructor.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

class_name

The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.

params

An optional array of parameters to pass to the constructor for class_name objects.

Return Values

Returns an object with string properties that corresponds to the fetched row or NULL if there are no more rows in resultset.

Note

Field names returned by this functionare case-sensitive.

Note

This function sets NULL fields tothe PHP NULL value.

Changelog

Version Description
5.0.0 Added the ability to return as a different object.

Examples

Example 22.197. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();} $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = $mysqli->query($query)) {    /* fetch object array */    while ($obj = $result->fetch_object()) {        printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);    }    /* free result set */    $result->close();}/* close connection */$mysqli->close();?>

Example 22.198. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = mysqli_query($link, $query)) {    /* fetch associative array */    while ($obj = mysqli_fetch_object($result)) {        printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);    }    /* free result set */    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Pueblo (USA)Arvada (USA)Cape Coral (USA)Green Bay (USA)Santa Clara (USA)

See Also

mysqli_fetch_array
mysqli_fetch_assoc
mysqli_fetch_row
mysqli_query
mysqli_data_seek

22.9.3.11.10. mysqli_result::fetch_row,mysqli_fetch_row

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::fetch_row

    mysqli_fetch_row

    Get a result row as an enumerated array

Description

Object oriented style

mixed mysqli_result::fetch_row();

Procedural style

mixed mysqli_fetch_row(mysqli_result result);

Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

mysqli_fetch_row returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in result set.

Note

This function sets NULL fields tothe PHP NULL value.

Examples

Example 22.199. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = $mysqli->query($query)) {    /* fetch object array */    while ($row = $result->fetch_row()) {        printf ("%s (%s)\n", $row[0], $row[1]);    }    /* free result set */    $result->close();}/* close connection */$mysqli->close();?>

Example 22.200. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = mysqli_query($link, $query)) {    /* fetch associative array */    while ($row = mysqli_fetch_row($result)) {        printf ("%s (%s)\n", $row[0], $row[1]);    }    /* free result set */    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Pueblo (USA)Arvada (USA)Cape Coral (USA)Green Bay (USA)Santa Clara (USA)

See Also

mysqli_fetch_array
mysqli_fetch_assoc
mysqli_fetch_object
mysqli_query
mysqli_data_seek

22.9.3.11.11. mysqli_result::$field_count,mysqli_num_fields

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::$field_count

    mysqli_num_fields

    Get the number of fields in a result

Description

Object oriented style

int mysqli_result->field_count ;

Procedural style

int mysqli_num_fields(mysqli_result result);

Returns the number of fields from specified result set.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

The number of fields from a result set.

Examples

Example 22.201. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}if ($result = $mysqli->query("SELECT * FROM City ORDER BY ID LIMIT 1")) {    /* determine number of fields in result set */    $field_cnt = $result->field_count;    printf("Result set has %d fields.\n", $field_cnt);    /* close result set */    $result->close();}/* close connection */$mysqli->close();?>

Example 22.202. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}if ($result = mysqli_query($link, "SELECT * FROM City ORDER BY ID LIMIT 1")) {    /* determine number of fields in result set */    $field_cnt = mysqli_num_fields($result);    printf("Result set has %d fields.\n", $field_cnt);    /* close result set */    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Result set has 5 fields.

See Also

mysqli_fetch_field

22.9.3.11.12. mysqli_result::field_seek,mysqli_field_seek

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::field_seek

    mysqli_field_seek

    Set result pointer to a specified field offset

Description

Object oriented style

bool mysqli_result::field_seek(int fieldnr);

Procedural style

bool mysqli_field_seek(mysqli_result result,
int fieldnr);

Sets the field cursor to the given offset. The next call to mysqli_fetch_field will retrieve the field definition of the column associated with that offset.

Note

To seek to the beginning of a row, pass an offset value of zero.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

fieldnr

The field number. This value must be in the range from 0 to number of fields - 1.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example 22.203. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = $mysqli->query($query)) {    /* Get field information for 2nd column */    $result->field_seek(1);    $finfo = $result->fetch_field();    printf("Name:     %s\n", $finfo->name);    printf("Table:    %s\n", $finfo->table);    printf("max. Len: %d\n", $finfo->max_length);    printf("Flags:    %d\n", $finfo->flags);    printf("Type:     %d\n\n", $finfo->type);    $result->close();}/* close connection */$mysqli->close();?>

Example 22.204. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";if ($result = mysqli_query($link, $query)) {    /* Get field information for 2nd column */    mysqli_field_seek($result, 1);    $finfo = mysqli_fetch_field($result);    printf("Name:     %s\n", $finfo->name);    printf("Table:    %s\n", $finfo->table);    printf("max. Len: %d\n", $finfo->max_length);    printf("Flags:    %d\n", $finfo->flags);    printf("Type:     %d\n\n", $finfo->type);    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Name:     SurfaceAreaTable:    Countrymax. Len: 10Flags:    32769Type:     4

See Also

mysqli_fetch_field

22.9.3.11.13. mysqli_result::free,mysqli_free_result

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::free

    mysqli_free_result

    Frees the memory associated with a result

Description

Object oriented style

void mysqli_result::free();
void mysqli_result::close();
void mysqli_result::free_result();

Procedural style

void mysqli_free_result(mysqli_result result);

Frees the memory associated with the result.

Note

You should always free your result with mysqli_free_result, when your result object is not needed anymore.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

No value is returned.

See Also

mysqli_query
mysqli_stmt_store_result
mysqli_store_result
mysqli_use_result

22.9.3.11.14. mysqli_result::$lengths,mysqli_fetch_lengths

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::$lengths

    mysqli_fetch_lengths

    Returns the lengths of the columns of the current row in the result set

Description

Object oriented style

array mysqli_result->lengths ;

Procedural style

array mysqli_fetch_lengths(mysqli_result result);

The mysqli_fetch_lengths function returns an array containing the lengths of every column of the current row within the result set.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

An array of integers representing the size of each column (not including any terminating null characters). FALSE if an error occurred.

mysqli_fetch_lengths is valid only for the current row of the result set. It returns FALSE if you call it before calling mysqli_fetch_row/array/object or after retrieving all rows in the result.

Examples

Example 22.205. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT * from Country ORDER BY Code LIMIT 1";if ($result = $mysqli->query($query)) {    $row = $result->fetch_row();    /* display column lengths */    foreach ($result->lengths as $i => $val) {        printf("Field %2d has Length %2d\n", $i+1, $val);    }    $result->close();}/* close connection */$mysqli->close();?>

Example 22.206. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}$query = "SELECT * from Country ORDER BY Code LIMIT 1";if ($result = mysqli_query($link, $query)) {    $row = mysqli_fetch_row($result);    /* display column lengths */    foreach (mysqli_fetch_lengths($result) as $i => $val) {        printf("Field %2d has Length %2d\n", $i+1, $val);    }    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Field  1 has Length  3Field  2 has Length  5Field  3 has Length 13Field  4 has Length  9Field  5 has Length  6Field  6 has Length  1Field  7 has Length  6Field  8 has Length  4Field  9 has Length  6Field 10 has Length  6Field 11 has Length  5Field 12 has Length 44Field 13 has Length  7Field 14 has Length  3Field 15 has Length  2

22.9.3.11.15. mysqli_result::$num_rows,mysqli_num_rows

Copyright 1997-2012 the PHP Documentation Group.

  • mysqli_result::$num_rows

    mysqli_num_rows

    Gets the number of rows in a result

Description

Object oriented style

int mysqli_result->num_rows ;

Procedural style

int mysqli_num_rows(mysqli_result result);

Returns the number of rows in the result set.

The behaviour of mysqli_num_rows depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows will not return the correct number of rows until all the rows in the result have been retrieved.

Parameters

result

Procedural style only: A result set identifier returned by mysqli_query, mysqli_store_result or mysqli_use_result.

Return Values

Returns number of rows in the result set.

Note

If the number of rows is greater than MAXINT , the number will be returned as a string.

Examples

Example 22.207. Object oriented style

<?php$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {    /* determine number of rows result set */    $row_cnt = $result->num_rows;    printf("Result set has %d rows.\n", $row_cnt);    /* close result set */    $result->close();}/* close connection */$mysqli->close();?>

Example 22.208. Procedural style

<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {    printf("Connect failed: %s\n", mysqli_connect_error());    exit();}if ($result = mysqli_query($link, "SELECT Code, Name FROM Country ORDER BY Name")) {    /* determine number of rows result set */    $row_cnt = mysqli_num_rows($result);    printf("Result set has %d rows.\n", $row_cnt);    /* close result set */    mysqli_free_result($result);}/* close connection */mysqli_close($link);?>   

The above examples will output:

Result set has 239 rows.

See Also

mysqli_affected_rows
mysqli_store_result
mysqli_use_result
mysqli_query