com doc/ja: Improve mysqli_fetch_* functions docs: reference/mysqli/mysqli_result/fetch-array.xml reference/ mysqli/mysqli_result/fetch-assoc.xml
reference/mysqli/mys qli_result/fetch-object.xml reference/mysqli/mysqli_res ult/fetch-row.xml
| From: | Yoshinari Takaoka | Date: | Tue, 23 Feb 2021 13:40:32 +0000 |
| Subject: | com doc/ja: Improve mysqli_fetch_* functions docs: reference/mysqli/mysqli_result/fetch-array.xml reference/ mysqli/mysqli_result/fetch-assoc.xml reference/mysqli/mys qli_result/fetch-object.xml reference/mysqli/mysqli_res ult/fetch-row.xml |
||
| Groups: | php.doc.ja | ||
| Request: | Send a blank email to doc-ja+get-6218@lists.php.net to get a copy of this message | ||
Commit: 8a0924ba9ef1ea9c2f1ac9cf72ea0e24acb03eda
Author: Yoshinari Takaoka <mumumu@mumumu.org> Tue, 23 Feb 2021 22:40:32 +0900
Parents: 263d0d6afcbc75d5c83dad4ae56b3de7c888b322
Branches: master
Link: http://git.php.net/?p=doc/ja.git;a=commitdiff;h=8a0924ba9ef1ea9c2f1ac9cf72ea0e24acb03eda
Log:
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 <afilina@gmail.com>
Closes GH-207
Changed paths:
M reference/mysqli/mysqli_result/fetch-array.xml
M reference/mysqli/mysqli_result/fetch-assoc.xml
M reference/mysqli/mysqli_result/fetch-object.xml
M reference/mysqli/mysqli_result/fetch-row.xml
diff --git a/reference/mysqli/mysqli_result/fetch-array.xml b/reference/mysqli/mysqli_result/fetch-array.xml index 74041a0c5c..b7af0f9864 100644 --- a/reference/mysqli/mysqli_result/fetch-array.xml +++ b/reference/mysqli/mysqli_result/fetch-array.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-array" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -23,14 +23,13 @@ <methodparam choice="opt"><type>int</type><parameter>resulttype</parameter><initializer>MYSQLI_BOTH</initializer></methodparam> </methodsynopsis> <para> - 取得した行に対応する配列を返します。 <parameter>result</parameter> - パラメータがあらわす結果セットに対して行がなければ、&null; を返します。 + 取得した行に対応する配列を返します。 + 結果セットに対して行がなければ、&null; を返します。 </para> <para> - <function>mysqli_fetch_array</function> は - <function>mysqli_fetch_row</function> 関数の拡張版です。データを数値添字の - 配列に格納することに加えて、<function>mysqli_fetch_array</function> - 関数は、フィールド名をキーとする連想配列にもデータを格納します。 + データを数値添字の + 配列に格納することに加えて、この関数は + 結果セットのフィールド名をキーとする連想配列にもデータを格納できます。 </para> &database.field-case; &database.fetch-null; @@ -72,7 +71,7 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - 取得した行に対応する文字列の配列を返します。結果セットにもう行がない場合には + 取得した行に対応する値の配列を返します。結果セットにもう行がない場合には &null; を返します。 </para> </refsect1> @@ -80,79 +79,56 @@ <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_array</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -if ($mysqli->connect_errno) { - printf("Connect failed: %s\n", $mysqli->connect_error); - exit(); -} +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 LIMIT 3"; +$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3"; $result = $mysqli->query($query); /* 数値添字配列 */ $row = $result->fetch_array(MYSQLI_NUM); -printf ("%s (%s)\n", $row[0], $row[1]); +printf("%s (%s)\n", $row[0], $row[1]); /* 連想配列 */ $row = $result->fetch_array(MYSQLI_ASSOC); -printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); +printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); /* 連想配列および数値添字配列 */ $row = $result->fetch_array(MYSQLI_BOTH); -printf ("%s (%s)\n", $row[0], $row["CountryCode"]); - -/* 結果セットを開放します */ -$result->free(); - -/* 接続を閉じます */ -$mysqli->close(); -?> +printf("%s (%s)\n", $row[0], $row["CountryCode"]); ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -if (mysqli_connect_errno()) { - printf("Connect failed: %s\n", mysqli_connect_error()); - exit(); -} +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); $query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3"; -$result = mysqli_query($link, $query); +$result = mysqli_query($mysqli, $query); /* 数値添字配列 */ $row = mysqli_fetch_array($result, MYSQLI_NUM); -printf ("%s (%s)\n", $row[0], $row[1]); +printf("%s (%s)\n", $row[0], $row[1]); /* 連想配列 */ $row = mysqli_fetch_array($result, MYSQLI_ASSOC); -printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); +printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); /* 連想配列および数値添字配列 */ $row = mysqli_fetch_array($result, MYSQLI_BOTH); -printf ("%s (%s)\n", $row[0], $row["CountryCode"]); - -/* 結果セットを開放します */ -mysqli_free_result($result); - -/* 接続を閉じます */ -mysqli_close($link); -?> +printf("%s (%s)\n", $row[0], $row["CountryCode"]); ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Kabul (AFG) diff --git a/reference/mysqli/mysqli_result/fetch-assoc.xml b/reference/mysqli/mysqli_result/fetch-assoc.xml index b45097b8d8..7b2a202490 100644 --- a/reference/mysqli/mysqli_result/fetch-assoc.xml +++ b/reference/mysqli/mysqli_result/fetch-assoc.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-assoc" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -22,7 +22,7 @@ <methodparam><type>mysqli_result</type><parameter>result</parameter></methodparam> </methodsynopsis> <para> - 取得した行に対応する連想配列を返します。もしもう行がない場合には + 取得した行に対応する連想配列を返します。もう行がない場合には &null; を返します。 </para> &database.field-case; @@ -41,7 +41,7 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - 取得した行に対応する文字列の連想配列を返します。 + 取得した行に対応する値の連想配列を返します。 連想配列の各キーが、結果セットのカラムを表します。 結果セットにもう行がない場合には &null; を返します。 </para> @@ -57,69 +57,44 @@ <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_assoc</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); - -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -if ($result = $mysqli->query($query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 連想配列を取得します */ - while ($row = $result->fetch_assoc()) { - printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); - } +$result = $mysqli->query($query); - /* 結果セットを開放します */ - $result->free(); +/* 連想配列を取得します */ +while ($row = $result->fetch_assoc()) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } - -/* 接続を閉じます */ -$mysqli->close(); -?> ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); - -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); -if ($result = mysqli_query($link, $query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 連想配列を取得します */ - while ($row = mysqli_fetch_assoc($result)) { - printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); - } +$result = mysqli_query($mysqli, $query); - /* 結果セットを開放します */ - mysqli_free_result($result); +/* 連想配列を取得します */ +while ($row = mysqli_fetch_assoc($result)) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } - -/* 接続を閉じます */ -mysqli_close($link); -?> ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Pueblo (USA) @@ -131,42 +106,51 @@ Santa Clara (USA) </screen> </example> <example xml:id="mysqli-result.example.iterator"> - <title><classname>mysqli_result</classname> での <classname>iterator</classname> の使用法</title> + <title><classname>mysqli_result</classname>, <classname>iterator</classname>, <methodname>mysqli_result::fetch_assoc</methodname> の使い方の比較</title> + <para> + <classname>mysqli_result</classname> は、&foreach; を使って繰り返し処理をすることができます。 + 結果セットは、現在の位置に関わらず、最初の行から処理されます。 + </para> <programlisting role="php"> <![CDATA[ <?php -$c = mysqli_connect('127.0.0.1','user', 'pass'); -// イテレータがあるとき (PHP 5.4 でサポートされるようになりました) -foreach ( $c->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'; + +// イテレータを使う場合 +$result = $mysqli->query($query); +foreach ($result as $row) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } echo "\n==================\n"; -// イテレータのないとき -$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"]); } - -?> ]]> </programlisting> &example.outputs.similar; <screen> <![CDATA[ -'root'@'192.168.1.1' -'root'@'127.0.0.1' -'dude'@'localhost' -'lebowski'@'localhost' +Pueblo (USA) +Arvada (USA) +Cape Coral (USA) +Green Bay (USA) +Santa Clara (USA) ================== +Pueblo (USA) +Arvada (USA) +Cape Coral (USA) +Green Bay (USA) +Santa Clara (USA) -'root'@'192.168.1.1' -'root'@'127.0.0.1' -'dude'@'localhost' -'lebowski'@'localhost' ]]> </screen> </example> diff --git a/reference/mysqli/mysqli_result/fetch-object.xml b/reference/mysqli/mysqli_result/fetch-object.xml index 5fee49c0b8..269ccdfe90 100644 --- a/reference/mysqli/mysqli_result/fetch-object.xml +++ b/reference/mysqli/mysqli_result/fetch-object.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-object" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -25,14 +25,17 @@ <methodparam choice="opt"><type>array</type><parameter>params</parameter></methodparam> </methodsynopsis> <para> - <function>mysqli_fetch_object</function> は、結果セットの現在の行を - オブジェクトで返します。このオブジェクトは、結果セットのフィールド名を - 属性として持ちます。 - </para> - <para> - <function>mysqli_fetch_object</function> がオブジェクトのプロパティを設定するのは、 - オブジェクトのコンストラクタをコールする前であることに注意しましょう。 + 結果セットの現在の行をオブジェクトで返します。 + このオブジェクトは、結果セットのフィールド名を属性として持ちます。 </para> + <note xmlns="http://docbook.org/ns/docbook"> + <simpara> + この関数がオブジェクトのプロパティを設定するのは、 + オブジェクトのコンストラクタをコールする前であることに注意しましょう。 + </simpara> + </note> + &database.field-case; + &database.fetch-null; </refsect1> <refsect1 role="parameters"> @@ -65,79 +68,52 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - 取得した行に対応する文字列プロパティを有するオブジェクトを返します。 - もし結果セットにもう行がない場合には &null; を返します。 + 取得した行に対応するオブジェクトを返します。 + 結果セットにもう行がない場合には &null; を返します。 </para> - &database.field-case; - &database.fetch-null; </refsect1> <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_object</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); - -/* 接続状況をチェックします */ -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)) { +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"; - /* オブジェクトの配列を取得します */ - while ($obj = $result->fetch_object()) { - printf ("%s (%s)\n", $obj->Name, $obj->CountryCode); - } +$result = $mysqli->query($query); - /* 結果セットを開放します */ - $result->close(); +/* オブジェクトの配列を取得します */ +while ($obj = $result->fetch_object()) { + printf("%s (%s)\n", $obj->Name, $obj->CountryCode); } - -/* 接続を閉じます */ -$mysqli->close(); -?> ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -if ($result = mysqli_query($link, $query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* オブジェクトの配列を取得します */ - while ($obj = mysqli_fetch_object($result)) { - printf ("%s (%s)\n", $obj->Name, $obj->CountryCode); - } +$result = mysqli_query($link, $query); - /* 結果セットを開放します */ - mysqli_free_result($result); +/* 連想配列を取得します */ +while ($obj = mysqli_fetch_object($result)) { + printf("%s (%s)\n", $obj->Name, $obj->CountryCode); } - -/* 接続を閉じます */ -mysqli_close($link); -?> ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Pueblo (USA) diff --git a/reference/mysqli/mysqli_result/fetch-row.xml b/reference/mysqli/mysqli_result/fetch-row.xml index 7076711576..128f00ed48 100644 --- a/reference/mysqli/mysqli_result/fetch-row.xml +++ b/reference/mysqli/mysqli_result/fetch-row.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-row" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -25,9 +25,10 @@ 結果セットからデータを 1 行取得し、それを数値添字の配列で 返します。各カラムは 0(ゼロ)から始まる添字に格納されます。 <function>mysqli_fetch_row</function> 関数を続けてコールすると、 - 結果セットの次の行を返します。もしもう行がない場合には + 結果セットの次の行を返します。もう行がない場合には &null; を返します。 </para> + &database.fetch-null; </refsect1> <refsect1 role="parameters"> @@ -42,78 +43,52 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - <function>mysqli_fetch_row</function> は取得した行に対応する文字列の配列を - 返します。もしもう行がない場合には &null; を返します。 + <function>mysqli_fetch_row</function> は取得した行に対応する値の配列を + 返します。もう行がない場合には &null; を返します。 </para> - &database.fetch-null; </refsect1> <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_row</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -if ($result = $mysqli->query($query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 配列を取得します */ - while ($row = $result->fetch_row()) { - printf ("%s (%s)\n", $row[0], $row[1]); - } +$result = $mysqli->query($query); - /* 結果セットを開放します */ - $result->close(); +/* オブジェクトの配列を取得します */ +while ($row = $result->fetch_row()) { + printf("%s (%s)\n", $row[0], $row[1]); } - -/* 接続を閉じます */ -$mysqli->close(); -?> ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); -if ($result = mysqli_query($link, $query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 配列を取得します */ - while ($row = mysqli_fetch_row($result)) { - printf ("%s (%s)\n", $row[0], $row[1]); - } +$result = mysqli_query($mysqli, $query); - /* 結果セットを開放します */ - mysqli_free_result($result); +/* 連想配列を取得します */ +while ($row = mysqli_fetch_row($result)) { + printf("%s (%s)\n", $row[0], $row[1]); } - -/* 接続を閉じます */ -mysqli_close($link); -?> ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Pueblo (USA)
diff --git a/reference/mysqli/mysqli_result/fetch-array.xml b/reference/mysqli/mysqli_result/fetch-array.xml index 74041a0c5c..b7af0f9864 100644 --- a/reference/mysqli/mysqli_result/fetch-array.xml +++ b/reference/mysqli/mysqli_result/fetch-array.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-array" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -23,14 +23,13 @@ <methodparam choice="opt"><type>int</type><parameter>resulttype</parameter><initializer>MYSQLI_BOTH</initializer></methodparam> </methodsynopsis> <para> - 取得した行に対応する配列を返します。 <parameter>result</parameter> - パラメータがあらわす結果セットに対して行がなければ、&null; を返します。 + 取得した行に対応する配列を返します。 + 結果セットに対して行がなければ、&null; を返します。 </para> <para> - <function>mysqli_fetch_array</function> は - <function>mysqli_fetch_row</function> 関数の拡張版です。データを数値添字の - 配列に格納することに加えて、<function>mysqli_fetch_array</function> - 関数は、フィールド名をキーとする連想配列にもデータを格納します。 + データを数値添字の + 配列に格納することに加えて、この関数は + 結果セットのフィールド名をキーとする連想配列にもデータを格納できます。 </para> &database.field-case; &database.fetch-null; @@ -72,7 +71,7 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - 取得した行に対応する文字列の配列を返します。結果セットにもう行がない場合には + 取得した行に対応する値の配列を返します。結果セットにもう行がない場合には &null; を返します。 </para> </refsect1> @@ -80,79 +79,56 @@ <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_array</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -if ($mysqli->connect_errno) { - printf("Connect failed: %s\n", $mysqli->connect_error); - exit(); -} +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 LIMIT 3"; +$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3"; $result = $mysqli->query($query); /* 数値添字配列 */ $row = $result->fetch_array(MYSQLI_NUM); -printf ("%s (%s)\n", $row[0], $row[1]); +printf("%s (%s)\n", $row[0], $row[1]); /* 連想配列 */ $row = $result->fetch_array(MYSQLI_ASSOC); -printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); +printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); /* 連想配列および数値添字配列 */ $row = $result->fetch_array(MYSQLI_BOTH); -printf ("%s (%s)\n", $row[0], $row["CountryCode"]); - -/* 結果セットを開放します */ -$result->free(); - -/* 接続を閉じます */ -$mysqli->close(); -?> +printf("%s (%s)\n", $row[0], $row["CountryCode"]); ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -if (mysqli_connect_errno()) { - printf("Connect failed: %s\n", mysqli_connect_error()); - exit(); -} +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); $query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3"; -$result = mysqli_query($link, $query); +$result = mysqli_query($mysqli, $query); /* 数値添字配列 */ $row = mysqli_fetch_array($result, MYSQLI_NUM); -printf ("%s (%s)\n", $row[0], $row[1]); +printf("%s (%s)\n", $row[0], $row[1]); /* 連想配列 */ $row = mysqli_fetch_array($result, MYSQLI_ASSOC); -printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); +printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); /* 連想配列および数値添字配列 */ $row = mysqli_fetch_array($result, MYSQLI_BOTH); -printf ("%s (%s)\n", $row[0], $row["CountryCode"]); - -/* 結果セットを開放します */ -mysqli_free_result($result); - -/* 接続を閉じます */ -mysqli_close($link); -?> +printf("%s (%s)\n", $row[0], $row["CountryCode"]); ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Kabul (AFG) diff --git a/reference/mysqli/mysqli_result/fetch-assoc.xml b/reference/mysqli/mysqli_result/fetch-assoc.xml index b45097b8d8..7b2a202490 100644 --- a/reference/mysqli/mysqli_result/fetch-assoc.xml +++ b/reference/mysqli/mysqli_result/fetch-assoc.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-assoc" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -22,7 +22,7 @@ <methodparam><type>mysqli_result</type><parameter>result</parameter></methodparam> </methodsynopsis> <para> - 取得した行に対応する連想配列を返します。もしもう行がない場合には + 取得した行に対応する連想配列を返します。もう行がない場合には &null; を返します。 </para> &database.field-case; @@ -41,7 +41,7 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - 取得した行に対応する文字列の連想配列を返します。 + 取得した行に対応する値の連想配列を返します。 連想配列の各キーが、結果セットのカラムを表します。 結果セットにもう行がない場合には &null; を返します。 </para> @@ -57,69 +57,44 @@ <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_assoc</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); - -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -if ($result = $mysqli->query($query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 連想配列を取得します */ - while ($row = $result->fetch_assoc()) { - printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); - } +$result = $mysqli->query($query); - /* 結果セットを開放します */ - $result->free(); +/* 連想配列を取得します */ +while ($row = $result->fetch_assoc()) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } - -/* 接続を閉じます */ -$mysqli->close(); -?> ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); - -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); -if ($result = mysqli_query($link, $query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 連想配列を取得します */ - while ($row = mysqli_fetch_assoc($result)) { - printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); - } +$result = mysqli_query($mysqli, $query); - /* 結果セットを開放します */ - mysqli_free_result($result); +/* 連想配列を取得します */ +while ($row = mysqli_fetch_assoc($result)) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } - -/* 接続を閉じます */ -mysqli_close($link); -?> ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Pueblo (USA) @@ -131,42 +106,51 @@ Santa Clara (USA) </screen> </example> <example xml:id="mysqli-result.example.iterator"> - <title><classname>mysqli_result</classname> での <classname>iterator</classname> の使用法</title> + <title><classname>mysqli_result</classname>, <classname>iterator</classname>, <methodname>mysqli_result::fetch_assoc</methodname> の使い方の比較</title> + <para> + <classname>mysqli_result</classname> は、&foreach; を使って繰り返し処理をすることができます。 + 結果セットは、現在の位置に関わらず、最初の行から処理されます。 + </para> <programlisting role="php"> <![CDATA[ <?php -$c = mysqli_connect('127.0.0.1','user', 'pass'); -// イテレータがあるとき (PHP 5.4 でサポートされるようになりました) -foreach ( $c->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'; + +// イテレータを使う場合 +$result = $mysqli->query($query); +foreach ($result as $row) { + printf("%s (%s)\n", $row["Name"], $row["CountryCode"]); } echo "\n==================\n"; -// イテレータのないとき -$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"]); } - -?> ]]> </programlisting> &example.outputs.similar; <screen> <![CDATA[ -'root'@'192.168.1.1' -'root'@'127.0.0.1' -'dude'@'localhost' -'lebowski'@'localhost' +Pueblo (USA) +Arvada (USA) +Cape Coral (USA) +Green Bay (USA) +Santa Clara (USA) ================== +Pueblo (USA) +Arvada (USA) +Cape Coral (USA) +Green Bay (USA) +Santa Clara (USA) -'root'@'192.168.1.1' -'root'@'127.0.0.1' -'dude'@'localhost' -'lebowski'@'localhost' ]]> </screen> </example> diff --git a/reference/mysqli/mysqli_result/fetch-object.xml b/reference/mysqli/mysqli_result/fetch-object.xml index 5fee49c0b8..269ccdfe90 100644 --- a/reference/mysqli/mysqli_result/fetch-object.xml +++ b/reference/mysqli/mysqli_result/fetch-object.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-object" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -25,14 +25,17 @@ <methodparam choice="opt"><type>array</type><parameter>params</parameter></methodparam> </methodsynopsis> <para> - <function>mysqli_fetch_object</function> は、結果セットの現在の行を - オブジェクトで返します。このオブジェクトは、結果セットのフィールド名を - 属性として持ちます。 - </para> - <para> - <function>mysqli_fetch_object</function> がオブジェクトのプロパティを設定するのは、 - オブジェクトのコンストラクタをコールする前であることに注意しましょう。 + 結果セットの現在の行をオブジェクトで返します。 + このオブジェクトは、結果セットのフィールド名を属性として持ちます。 </para> + <note xmlns="http://docbook.org/ns/docbook"> + <simpara> + この関数がオブジェクトのプロパティを設定するのは、 + オブジェクトのコンストラクタをコールする前であることに注意しましょう。 + </simpara> + </note> + &database.field-case; + &database.fetch-null; </refsect1> <refsect1 role="parameters"> @@ -65,79 +68,52 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - 取得した行に対応する文字列プロパティを有するオブジェクトを返します。 - もし結果セットにもう行がない場合には &null; を返します。 + 取得した行に対応するオブジェクトを返します。 + 結果セットにもう行がない場合には &null; を返します。 </para> - &database.field-case; - &database.fetch-null; </refsect1> <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_object</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); - -/* 接続状況をチェックします */ -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)) { +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"; - /* オブジェクトの配列を取得します */ - while ($obj = $result->fetch_object()) { - printf ("%s (%s)\n", $obj->Name, $obj->CountryCode); - } +$result = $mysqli->query($query); - /* 結果セットを開放します */ - $result->close(); +/* オブジェクトの配列を取得します */ +while ($obj = $result->fetch_object()) { + printf("%s (%s)\n", $obj->Name, $obj->CountryCode); } - -/* 接続を閉じます */ -$mysqli->close(); -?> ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -if ($result = mysqli_query($link, $query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* オブジェクトの配列を取得します */ - while ($obj = mysqli_fetch_object($result)) { - printf ("%s (%s)\n", $obj->Name, $obj->CountryCode); - } +$result = mysqli_query($link, $query); - /* 結果セットを開放します */ - mysqli_free_result($result); +/* 連想配列を取得します */ +while ($obj = mysqli_fetch_object($result)) { + printf("%s (%s)\n", $obj->Name, $obj->CountryCode); } - -/* 接続を閉じます */ -mysqli_close($link); -?> ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Pueblo (USA) diff --git a/reference/mysqli/mysqli_result/fetch-row.xml b/reference/mysqli/mysqli_result/fetch-row.xml index 7076711576..128f00ed48 100644 --- a/reference/mysqli/mysqli_result/fetch-row.xml +++ b/reference/mysqli/mysqli_result/fetch-row.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: c1434c95fc666c20cdbd1e1d9a4183e0d11fb88c Maintainer: takagi Status: ready --> +<!-- EN-Revision: ca5c477437fcb21680408bf7e0cc0424853e7907 Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="mysqli-result.fetch-row" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -25,9 +25,10 @@ 結果セットからデータを 1 行取得し、それを数値添字の配列で 返します。各カラムは 0(ゼロ)から始まる添字に格納されます。 <function>mysqli_fetch_row</function> 関数を続けてコールすると、 - 結果セットの次の行を返します。もしもう行がない場合には + 結果セットの次の行を返します。もう行がない場合には &null; を返します。 </para> + &database.fetch-null; </refsect1> <refsect1 role="parameters"> @@ -42,78 +43,52 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - <function>mysqli_fetch_row</function> は取得した行に対応する文字列の配列を - 返します。もしもう行がない場合には &null; を返します。 + <function>mysqli_fetch_row</function> は取得した行に対応する値の配列を + 返します。もう行がない場合には &null; を返します。 </para> - &database.fetch-null; </refsect1> <refsect1 role="examples"> &reftitle.examples; <example> - <title>&style.oop;</title> + <title><methodname>mysqli_result::fetch_row</methodname> の例</title> + <para>&style.oop;</para> <programlisting role="php"> <![CDATA[ <?php -$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); -if ($result = $mysqli->query($query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 配列を取得します */ - while ($row = $result->fetch_row()) { - printf ("%s (%s)\n", $row[0], $row[1]); - } +$result = $mysqli->query($query); - /* 結果セットを開放します */ - $result->close(); +/* オブジェクトの配列を取得します */ +while ($row = $result->fetch_row()) { + printf("%s (%s)\n", $row[0], $row[1]); } - -/* 接続を閉じます */ -$mysqli->close(); -?> ]]> </programlisting> - </example> - <example> - <title>&style.procedural;</title> + <para>&style.procedural;</para> <programlisting role="php"> <![CDATA[ <?php -$link = mysqli_connect("localhost", "my_user", "my_password", "world"); -/* 接続状況をチェックします */ -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"; +mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); +$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world"); -if ($result = mysqli_query($link, $query)) { +$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC"; - /* 配列を取得します */ - while ($row = mysqli_fetch_row($result)) { - printf ("%s (%s)\n", $row[0], $row[1]); - } +$result = mysqli_query($mysqli, $query); - /* 結果セットを開放します */ - mysqli_free_result($result); +/* 連想配列を取得します */ +while ($row = mysqli_fetch_row($result)) { + printf("%s (%s)\n", $row[0], $row[1]); } - -/* 接続を閉じます */ -mysqli_close($link); -?> ]]> </programlisting> - &examples.outputs; + &examples.outputs.similar; <screen> <![CDATA[ Pueblo (USA)