From ca5c477437fcb21680408bf7e0cc0424853e7907 Mon Sep 17 00:00:00 2001 From: Dharman Date: Sat, 9 Jan 2021 17:47:58 +0100 Subject: [PATCH] Improve mysqli_fetch_* functions docs * Aligned examples. OO and procedural examples where using different variable names. An example of iterator was using different style and data * Improved descriptions. Some words were confusing and even wrong. * Aligned notes. All notes are in the same place and follow similar grammar * Removed manual error checking from examples * Removed unnecessary LIMIT 50,5 * Added .similar to the example results Co-authored-by: Anna Filina Closes GH-207 --- .../mysqli/mysqli_result/fetch-array.xml | 70 ++++------- .../mysqli/mysqli_result/fetch-assoc.xml | 118 ++++++++---------- .../mysqli/mysqli_result/fetch-object.xml | 85 +++++-------- reference/mysqli/mysqli_result/fetch-row.xml | 75 ++++------- 4 files changed, 129 insertions(+), 219 deletions(-) diff --git a/reference/mysqli/mysqli_result/fetch-array.xml b/reference/mysqli/mysqli_result/fetch-array.xml index 993a796248..d790051e4a 100644 --- a/reference/mysqli/mysqli_result/fetch-array.xml +++ b/reference/mysqli/mysqli_result/fetch-array.xml @@ -22,15 +22,12 @@ 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. + are no more rows for the result set. - 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. + In addition to storing the data in the numeric indices of the result array, + this function can also store the data in associative indices + by using the field names of the result set as keys. &database.field-case; &database.fetch-null; @@ -73,87 +70,64 @@ &reftitle.returnvalues; - Returns an array of strings that corresponds to the fetched row or &null; if there - are no more rows in resultset. + Returns an array of values that corresponds to the fetched row or &null; if there + are no more rows in result set. &reftitle.examples; - &style.oop; + <methodname>mysqli_result::fetch_array</methodname> example + &style.oop; connect_errno) { - printf("Connect failed: %s\n", $mysqli->connect_error); - exit(); -} - -$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3"; +$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]); +printf("%s (%s)\n", $row[0], $row[1]); /* associative array */ $row = $result->fetch_array(MYSQLI_ASSOC); -printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); +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(); -?> +printf("%s (%s)\n", $row[0], $row["CountryCode"]); ]]> - - - &style.procedural; + &style.procedural; +printf("%s (%s)\n", $row[0], $row["CountryCode"]); ]]> - &examples.outputs; + &examples.outputs.similar; &reftitle.returnvalues; - Returns an associative array of strings representing the fetched row in the result + Returns an associative array of values 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. + set's columns or &null; if there are no more rows in result set. If two or more columns of the result have the same field names, the last @@ -54,69 +54,44 @@ &reftitle.examples; - &style.oop; + <methodname>mysqli_result::fetch_assoc</methodname> example + &style.oop; connect_errno) { - printf("Connect failed: %s\n", $mysqli->connect_error); - exit(); +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; + +$result = $mysqli->query($query); + +/* fetch associative array */ +while ($row = $result->fetch_assoc()) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } - -$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(); -?> ]]> - - - &style.procedural; + &style.procedural; ]]> - &examples.outputs; + &examples.outputs.similar; - A <classname>mysqli_result</classname> example comparing <classname>iterator</classname> usage + Comparison of <classname>mysqli_result</classname> <classname>iterator</classname> and <methodname>mysqli_result::fetch_assoc</methodname> usage + + mysqli_result can be iterated using &foreach;. + The result set will always be iterated from the first row, regardless of the current position. + query('SELECT user,host FROM mysql.user') as $row ) { - printf("'%s'@'%s'\n", $row['user'], $row['host']); +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); + +$query = 'SELECT Name, CountryCode FROM City ORDER BY ID DESC'; + +// Using iterators +$result = $mysqli->query($query); +foreach ($result as $row) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } echo "\n==================\n"; // Not using iterators -$result = $c->query('SELECT user,host FROM mysql.user'); +$result = $mysqli->query($query); while ($row = $result->fetch_assoc()) { - printf("'%s'@'%s'\n", $row['user'], $row['host']); + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } - -?> ]]> &example.outputs.similar; diff --git a/reference/mysqli/mysqli_result/fetch-object.xml b/reference/mysqli/mysqli_result/fetch-object.xml index 8bae4d918a..fe2a4139a7 100644 --- a/reference/mysqli/mysqli_result/fetch-object.xml +++ b/reference/mysqli/mysqli_result/fetch-object.xml @@ -23,14 +23,18 @@ arrayparams - The mysqli_fetch_object will return the current row + Returns 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. - + + + This function sets the properties + of the object before calling the object constructor. + + + &database.field-case; + &database.fetch-null; @@ -63,79 +67,52 @@ &reftitle.returnvalues; - Returns an object with string properties that corresponds to the fetched - row or &null; if there are no more rows in resultset. + Returns an object that corresponds to the fetched + row or &null; if there are no more rows in result set. - &database.field-case; - &database.fetch-null; &reftitle.examples; - &style.oop; + <methodname>mysqli_result::fetch_object</methodname> example + &style.oop; query($query)) { +$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(); +/* fetch object array */ +while ($obj = $result->fetch_object()) { + printf("%s (%s)\n", $obj->Name, $obj->CountryCode); } - -/* close connection */ -$mysqli->close(); -?> ]]> - - - &style.procedural; + &style.procedural; Name, $obj->CountryCode); } - -$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); -?> ]]> - &examples.outputs; + &examples.outputs.similar; + &database.fetch-null; @@ -39,78 +40,52 @@ &reftitle.returnvalues; - mysqli_fetch_row returns an array of strings that corresponds to the fetched row + mysqli_fetch_row returns an array of values that corresponds to the fetched row or &null; if there are no more rows in result set. - &database.fetch-null; &reftitle.examples; - &style.oop; + <methodname>mysqli_result::fetch_row</methodname> example + &style.oop; query($query); + +/* fetch object array */ +while ($row = $result->fetch_row()) { + printf("%s (%s)\n", $row[0], $row[1]); } - -$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(); -?> ]]> - - - &style.procedural; + &style.procedural; ]]> - &examples.outputs; + &examples.outputs.similar;