reference/mysqli/mysqli_result/data-seek.xml
035c126c0393fe154bac46e2c3c489ebadce48a5
...
...
@@ -10,12 +10,12 @@
10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
12
<para>&style.oop;</para>
13
-
<methodsynopsis role="oop">
13
+
<methodsynopsis role="mysqli_result">
14
14
<modifier>public</modifier> <type>bool</type><methodname>mysqli_result::data_seek</methodname>
15
15
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
16
16
</methodsynopsis>
17
17
<para>&style.procedural;</para>
18
-
<methodsynopsis role="procedural">
18
+
<methodsynopsis>
19
19
<type>bool</type><methodname>mysqli_data_seek</methodname>
20
20
<methodparam><type>mysqli_result</type><parameter>result</parameter></methodparam>
21
21
<methodparam><type>int</type><parameter>offset</parameter></methodparam>
...
...
@@ -36,7 +36,7 @@
36
36
<term><parameter>offset</parameter></term>
37
37
<listitem>
38
38
<para>
39
-
The field offset. Must be between zero and the total number of rows
39
+
The row offset. Must be between zero and the total number of rows
40
40
minus one (0..<function>mysqli_num_rows</function> - 1).
41
41
</para>
42
42
</listitem>
...
...
@@ -55,79 +55,105 @@
55
55
<refsect1 role="examples">
56
56
&reftitle.examples;
57
57
<example>
58
-
<title>&style.oop;</title>
58
+
<title><methodname>mysqli::data_seek</methodname> example</title>
59
+
<para>&style.oop;</para>
59
60
<programlisting role="php">
60
61
<![CDATA[
61
62
<?php
62
-
/* Open a connection */
63
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
64
63

65
-
/* check connection */
66
-
if (mysqli_connect_errno()) {
67
-
printf("Connect failed: %s\n", mysqli_connect_error());
68
-
exit();
69
-
}
64
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
65
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
70
66

71
67
$query = "SELECT Name, CountryCode FROM City ORDER BY Name";
72
-
if ($result = $mysqli->query($query)) {
68
+
$result = $mysqli->query($query);
73
69

74
-
/* seek to row no. 400 */
75
-
$result->data_seek(399);
70
+
/* Seek to row no. 401 */
71
+
$result->data_seek(400);
76
72

77
-
/* fetch row */
78
-
$row = $result->fetch_row();
73
+
/* Fetch single row */
74
+
$row = $result->fetch_row();
79
75

80
-
printf ("City: %s Countrycode: %s\n", $row[0], $row[1]);
76
+
printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
77
+
]]>
78
+
</programlisting>
79
+
<para>&style.procedural;</para>
80
+
<programlisting role="php">
81
+
<![CDATA[
82
+
<?php
81
83

82
-
/* free result set*/
83
-
$result->close();
84
-
}
84
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
85
+
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
86
+

87
+
$query = "SELECT Name, CountryCode FROM City ORDER BY Name";
85
88

86
-
/* close connection */
87
-
$mysqli->close();
88
-
?>
89
+
$result = mysqli_query($link, $query);
90
+

91
+
/* Seek to row no. 401 */
92
+
mysqli_data_seek($result, 400);
93
+

94
+
/* Fetch single row */
95
+
$row = mysqli_fetch_row($result);
96
+

97
+
printf ("City: %s Countrycode: %s\n", $row[0], $row[1]);
89
98
]]>
90
99
</programlisting>
100
+
&examples.outputs;
101
+
<screen>
102
+
<![CDATA[
103
+
City: Benin City Countrycode: NGA
104
+
]]>
105
+
</screen>
91
106
</example>
107
+

92
108
<example>
93
-
<title>&style.procedural;</title>
109
+
<title>Adjusting the result pointer when iterating</title>
110
+
<para>
111
+
This function can be useful when iterating over the result set to impose
112
+
a custom order or rewind the result set when iterating multiple times.
113
+
</para>
94
114
<programlisting role="php">
95
115
<![CDATA[
96
116
<?php
97
-
/* Open a connection */
98
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
99
117

100
-
/* check connection */
101
-
if (!$link) {
102
-
printf("Connect failed: %s\n", mysqli_connect_error());
103
-
exit();
104
-
}
118
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
119
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
105
120

106
-
$query = "SELECT Name, CountryCode FROM City ORDER BY Name";
121
+
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 15,4";
122
+
$result = $mysqli->query($query);
123
+

124
+
/* Iterate the result set in reverse order */
125
+
for ($row_no = $result->num_rows - 1; $row_no >= 0; $row_no--) {
126
+
$result->data_seek($row_no);
107
127

108
-
if ($result = mysqli_query($link, $query)) {
128
+
/* Fetch single row */
129
+
$row = $result->fetch_row();
109
130

110
-
/* seek to row no. 400 */
111
-
mysqli_data_seek($result, 399);
131
+
printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
132
+
}
112
133

113
-
/* fetch row */
114
-
$row = mysqli_fetch_row($result);
134
+
/* Reset pointer to the beginning of the result set */
135
+
$result->data_seek(0);
115
136

116
-
printf ("City: %s Countrycode: %s\n", $row[0], $row[1]);
137
+
print "\n";
117
138

118
-
/* free result set*/
119
-
mysqli_free_result($result);
139
+
/* Iterate the same result set again */
140
+
while ($row = $result->fetch_row()) {
141
+
printf("City: %s Countrycode: %s\n", $row[0], $row[1]);
120
142
}
121
-

122
-
/* close connection */
123
-
mysqli_close($link);
124
-
?>
125
143
]]>
126
144
</programlisting>
127
145
&examples.outputs;
128
146
<screen>
129
147
<![CDATA[
130
-
City: Benin City Countrycode: NGA
148
+
City: Acmbaro Countrycode: MEX
149
+
City: Abuja Countrycode: NGA
150
+
City: Abu Dhabi Countrycode: ARE
151
+
City: Abottabad Countrycode: PAK
152
+

153
+
City: Abottabad Countrycode: PAK
154
+
City: Abu Dhabi Countrycode: ARE
155
+
City: Abuja Countrycode: NGA
156
+
City: Acmbaro Countrycode: MEX
131
157
]]>
132
158
</screen>
133
159
</example>
...
...
@@ -138,8 +164,8 @@ City: Benin City Countrycode: NGA
138
164
<note>
139
165
<para>
140
166
This function can only be used with buffered results attained from the
141
-
use of the <function>mysqli_store_result</function> or
142
-
<function>mysqli_query</function> functions.
167
+
use of the <function>mysqli_store_result</function>,
168
+
<function>mysqli_query</function> or <function>mysqli_stmt_get_result</function> functions.
143
169
</para>
144
170
</note>
145
171
</refsect1>
146
172