com doc/ja: Remove try-catch from PDO examples: reference/pdo/pdo/construct.xml reference/pdo/pdostatement/bindcolumn.xml
reference/pdo/pdostatement/fetch.xml reference/pdo/pdostatement/setfetchmode.xml

From: Date: Tue, 09 Mar 2021 11:18:45 +0000
Subject: com doc/ja: Remove try-catch from PDO examples: reference/pdo/pdo/construct.xml reference/pdo/pdostatement/bindcolumn.xml
reference/pdo/pdostatement/fetch.xml reference/pdo/pdostatement/setfetchmode.xml
Groups: php.doc.ja 
Request: Send a blank email to doc-ja+get-6241@lists.php.net to get a copy of this message
Commit: 832a1c0e76f925399e5c641f32affb3870a6a51d Author: Yoshinari Takaoka <mumumu@mumumu.org> Tue, 9 Mar 2021 20:18:45 +0900 Parents: 612f97c95a7f1fa7b155911294af8ada31ff9747 Branches: master Link: http://git.php.net/?p=doc/ja.git;a=commitdiff;h=832a1c0e76f925399e5c641f32affb3870a6a51d Log: Remove try-catch from PDO examples Also fix erroneous indentation. Co-authored-by: Anna Filina <afilina@gmail.com> Closes GH-323. Changed paths: M reference/pdo/pdo/construct.xml M reference/pdo/pdostatement/bindcolumn.xml M reference/pdo/pdostatement/fetch.xml M reference/pdo/pdostatement/setfetchmode.xml Diff: diff --git a/reference/pdo/pdo/construct.xml b/reference/pdo/pdo/construct.xml index 480b71f84c..e4ea4c375b 100644 --- a/reference/pdo/pdo/construct.xml +++ b/reference/pdo/pdo/construct.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: 7f569e1f175adf1fbd0e462eba8d6bc82214c939 Maintainer: takagi Status: ready --> +<!-- EN-Revision: a3a310ccc23af2710c307fb4c6535b123ae2223c Maintainer: takagi Status: ready --> <!-- CREDITS: hirokawa,shimooka,mumumu --> <refentry xml:id="pdo.construct" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -117,6 +117,7 @@ <para> <function>PDO::__construct</function> は、 指定されたデータベースへの接続に失敗した場合、 + <constant>PDO::ATTR_ERRMODE</constant> が設定されているかどうかに関わらず、 <classname>PDOException</classname> を投げます。 </para> </refsect1> @@ -133,11 +134,7 @@ $dsn = 'mysql:dbname=testdb;host=127.0.0.1'; $user = 'dbuser'; $password = 'dbpass'; -try { - $dbh = new PDO($dsn, $user, $password); -} catch (PDOException $e) { - echo 'Connection failed: ' . $e->getMessage(); -} +$dbh = new PDO($dsn, $user, $password); ?> ]]> @@ -167,11 +164,7 @@ $dsn = 'uri:file:///usr/local/dbconnect'; $user = ''; $password = ''; -try { - $dbh = new PDO($dsn, $user, $password); -} catch (PDOException $e) { - echo 'Connection failed: ' . $e->getMessage(); -} +$dbh = new PDO($dsn, $user, $password); ?> ]]> @@ -195,11 +188,7 @@ $dsn = 'mydb'; $user = ''; $password = ''; -try { - $dbh = new PDO($dsn, $user, $password); -} catch (PDOException $e) { - echo '接続に失敗しました: ' . $e->getMessage(); -} +$dbh = new PDO($dsn, $user, $password); ?> ]]> diff --git a/reference/pdo/pdostatement/bindcolumn.xml b/reference/pdo/pdostatement/bindcolumn.xml index 231cb405b7..74a439e377 100644 --- a/reference/pdo/pdostatement/bindcolumn.xml +++ b/reference/pdo/pdostatement/bindcolumn.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: 14af302c9c0e561fa6f9cdd956268758ba9a89c5 Maintainer: takagi Status: ready --> +<!-- EN-Revision: a3a310ccc23af2710c307fb4c6535b123ae2223c Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka --> <refentry xml:id="pdostatement.bindcolumn" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -122,33 +122,23 @@ <programlisting role="php"> <![CDATA[ <?php -function readData($dbh) { - $sql = 'SELECT name, colour, calories FROM fruit'; - try { - $stmt = $dbh->prepare($sql); - $stmt->execute(); +$stmt = $dbh->prepare('SELECT name, colour, calories FROM fruit'); +$stmt->execute(); - /* カラム番号によってバインドする */ - $stmt->bindColumn(1, $name); - $stmt->bindColumn(2, $colour); - - /* カラム名によってバインドする */ - $stmt->bindColumn('calories', $cals); +/* カラム番号によってバインドする */ +$stmt->bindColumn(1, $name); +$stmt->bindColumn(2, $colour); - while ($row = $stmt->fetch(PDO::FETCH_BOUND)) { - $data = $name . "\t" . $colour . "\t" . $cals . "\n"; - print $data; - } - } - catch (PDOException $e) { - print $e->getMessage(); - } +/* カラム名によってバインドする */ +$stmt->bindColumn('calories', $cals); + +while ($stmt->fetch(PDO::FETCH_BOUND)) { + print $name . "\t" . $colour . "\t" . $cals . "\n"; } -readData($dbh); ?> ]]> </programlisting> - &example.outputs; + &example.outputs.similar; <screen> <![CDATA[ apple red 150 diff --git a/reference/pdo/pdostatement/fetch.xml b/reference/pdo/pdostatement/fetch.xml index ff9968da7c..84c775ff4d 100644 --- a/reference/pdo/pdostatement/fetch.xml +++ b/reference/pdo/pdostatement/fetch.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> -<!-- EN-Revision: e440c79af15cf829549736b2a292b5f8f6887142 Maintainer: takagi Status: ready --> -<!-- CREDITS: hirokawa --> +<!-- EN-Revision: a3a310ccc23af2710c307fb4c6535b123ae2223c Maintainer: takagi Status: ready --> +<!-- CREDITS: hirokawa,mumumu --> <refentry xml:id="pdostatement.fetch" xmlns="http://docbook.org/ns/docbook"> <refnamediv> <refname>PDOStatement::fetch</refname> @@ -219,35 +219,23 @@ kiwi <![CDATA[ <?php function readDataForwards($dbh) { - $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY BET'; - try { + $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY BET'; $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { - $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n"; - print $data; + $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n"; + print $data; } - $stmt = null; - } - catch (PDOException $e) { - print $e->getMessage(); - } } function readDataBackwards($dbh) { - $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY bet'; - try { + $sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY bet'; $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST); do { - $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n"; - print $data; + $data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n"; + print $data; } while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR)); - $stmt = null; - } - catch (PDOException $e) { - print $e->getMessage(); - } } print "Reading forwards:\n"; diff --git a/reference/pdo/pdostatement/setfetchmode.xml b/reference/pdo/pdostatement/setfetchmode.xml index f3c89f34ab..736e1a6993 100644 --- a/reference/pdo/pdostatement/setfetchmode.xml +++ b/reference/pdo/pdostatement/setfetchmode.xml @@ -1,6 +1,6 @@ <?xml version="1..0" encoding="utf-8" ?> <!-- $Revision$ --> -<!-- EN-Revision: 9267d8b70e8fa541c5d464e56f94fef7b1b1a849 Maintainer: takagi Status: ready --> +<!-- EN-Revision: a3a310ccc23af2710c307fb4c6535b123ae2223c Maintainer: takagi Status: ready --> <!-- CREDITS: shimooka,mumumu --> <refentry xml:id="pdostatement.setfetchmode" xmlns="http://docbook.org/ns/docbook"> <refnamediv> @@ -105,18 +105,11 @@ <programlisting role="php"> <![CDATA[ <?php -$sql = 'SELECT name, colour, calories FROM fruit'; -try { - $stmt = $dbh->query($sql); - $result = $stmt->setFetchMode(PDO::FETCH_NUM); - while ($row = $stmt->fetch()) { +$stmt = $dbh->query('SELECT name, colour, calories FROM fruit'); +$stmt->setFetchMode(PDO::FETCH_NUM); +foreach ($stmt as $row) { print $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n"; - } } -catch (PDOException $e) { - print $e->getMessage(); -} -?> ]]> </programlisting> &example.outputs; @@ -128,7 +121,6 @@ orange orange 300 kiwi brown 75 lemon yellow 25 pear green 150 -watermelon pink 90 ]]> </screen> </example>

« previous php.doc.ja (#6241) next »