MENU
The mysqli result class
This section documents the functions for the mysqli_result object, which is returned by queries. With some exceptions, the procedural form of each function can be obtained by prepending each function name with mysqli_ and passing the mysqli_result object as the first parameter. For example, the procedural form of fetch_row() is mysqli_fetch_row($result).fetch_row() gets the next row as an numeric array. fetch_assoc() gets the next row as an associative array. fetch_array([$i = MYSQLI_BOTH]) gets the next row as an associative array, a numeric array, or both. $i can be MYSQLI_BOTH, MYSQLI_NUM, or MYSQLI_ASSOC. fetch_object([$s1 [, $s2]]) gets the current row as an object. $s1 is the name of the class to instantiate. $s2 is an optional array which passes arguments to the constructor $s1. fetch_all([$i =MYSQLI_NUM]) fetches all rows and gives an array of numeric or associative arrays. $i can be MYSQLI_BOTH, MYSQLI_NUM, or MYSQLI_ASSOC. data_seek($i) sets the pointer to a row specified by the offset $i.
fetch_field() returns the next field as an object. fetch_fields() gives an array of all field objects. $current_field gets the current field offset as an integer. The procedural form is mysqli_field_tell ($result). field_seek($i) brings the field cursor to the offset $i.
$num_rows gives the no. of resultant rows.
$field_count gives the no. of resultant fields. The procedural form is mysqli_num_fields($result). $lengths returns an array with the lengths of all columns for the current row. The procedural form is mysqli_fetch_lengths($result).
free(), close(), or free_result() clears the memory used for a result. The procedural form is mysqli_free_result($result).
<!DOCTYPE html><html><head></head><body><pre>
<?php
// S for SQL
$S=new mysqli("localhost","root","password","testDB");
$S->query("
DROP TABLE IF EXISTS tbl");
$S->query("
CREATE TABLE tbl (
a INT,
b VARCHAR(20))");
$S->query("
INSERT INTO tbl VALUES
(0,'Hello World'),
(1,'Testing 123'),
(2,'Good Day!'),
(3,'How Are You?'),
(4,'Good Luck!')");
// R for result
$R = $S->query("SELECT * FROM tbl");
$row = $R->fetch_row();
echo $row[0]."|".$row[1]."<br/>";
$row = $R->fetch_assoc();
echo $row['a']."|".$row['b']."<br/>";
$row = $R->fetch_array();
echo $row[0]."|".$row['b']."<br/>";
class C{
public $c=5;
}
$row = $R->fetch_object("C");
echo $row->a."|".$row->b."|".$row->c."<br/>";
$R->data_seek(3);
$row = $R->fetch_all();
print_r($row);
while ($field = $R->fetch_field()){
echo "\n".$R->current_field."|";
print_r($field);
}
free($R);
?>
</pre></body></html>
0|Hello World 1|Testing 123 2|Good Day! 3|How Are You?|5 Array ( [0] => Array ( [0] => 3 [1] => How Are You? ) [1] => Array ( [0] => 4 [1] => Good Luck! ) ) 1|stdClass Object ( [name] => a [orgname] => a [table] => tbl [orgtable] => tbl [def] => [db] => test [catalog] => def [max_length] => 12 [length] => 11 [charsetnr] => 63 [flags] => 32768 [type] => 3 [decimals] => 0 ) 2|stdClass Object ( [name] => b [orgname] => b [table] => tbl [orgtable] => tbl [def] => [db] => test [catalog] => def [max_length] => 0 [length] => 60 [charsetnr] => 33 [flags] => 0 [type] => 253 [decimals] => 0 )