cvs: peardoc /en/package/database/db intro-fetch.xml
| From: | Daniel Convissor | Date: | Mon, 02 Feb 2004 04:26:03 +0000 |
| Subject: | cvs: peardoc /en/package/database/db intro-fetch.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-2307@lists.php.net to get a copy of this message | ||
danielc Sun Feb 1 23:26:03 2004 EDT
Modified files:
/peardoc/en/package/database/db intro-fetch.xml
Log:
Clean up examples: fix index key numbers in results, add php tags, general formatting.
http://cvs.php.net/diff.php/peardoc/en/package/database/db/intro-fetch.xml?r1=1.12&r2=1.13&ty=u Index: peardoc/en/package/database/db/intro-fetch.xml diff -u peardoc/en/package/database/db/intro-fetch.xml:1.12 peardoc/en/package/database/db/intro-fetch.xml:1.13 --- peardoc/en/package/database/db/intro-fetch.xml:1.12 Tue Jan 27 14:12:57 2004 +++ peardoc/en/package/database/db/intro-fetch.xml Sun Feb 1 23:26:03 2004 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.12 $ --> +<!-- $Revision: 1.13 $ --> <refentry id="package.database.db.intro-fetch"> <refnamediv> <refname>Introduction - Fetch & Get</refname> @@ -30,15 +30,15 @@ <programlisting role="php"> <![CDATA[ <?php -... -$db = DB::connect($dsn); -$res = $db->query("SELECT * FROM mytable"); +// Once you have a valid DB object named $db... +$res =& $db->query("SELECT * FROM mytable"); // Get each row of data on each iteration until // there are no more rows -while ($row = $res->fetchRow()) { +while ($row =& $res->fetchRow()) { $id = $row[0]; } + // Or: // an example using fetchInto() while ($res->fetchInto($row)) { @@ -66,16 +66,19 @@ <programlisting role="php"> <![CDATA[ <?php -$res = $db->query('SELECT id, name, email FROM users'); -$row = $res->fetchRow(DB_FETCHMODE_ORDERED); +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT id, name, email FROM users'); +$row =& $res->fetchRow(DB_FETCHMODE_ORDERED); + /* -$row will contain: -array ( - 0 => <column "id" data>, - 1 => <column "name" data>, - 2 => <column "email" data> -) -*/ + * $row will contain: + * array ( + * 0 => <column "id" data>, + * 1 => <column "name" data>, + * 2 => <column "email" data> + * ) + */ + // Access the data with: $id = $row[0]; $name = $row[1]; @@ -98,16 +101,19 @@ <programlisting role="php"> <![CDATA[ <?php -$res = $db->query('SELECT id, name, email FROM users'); -$row = $res->fetchRow(DB_FETCHMODE_ASSOC); +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT id, name, email FROM users'); +$row =& $res->fetchRow(DB_FETCHMODE_ASSOC); + /* -$row will contain: -array ( - 'id' => <column "id" data>, - 'name' => <column "name" data>, - 'email' => <column "email" data> -) -*/ + * $row will contain: + * array ( + * 'id' => <column "id" data>, + * 'name' => <column "name" data>, + * 'email' => <column "email" data> + * ) + */ + // Access the data with: $id = $row['id']; $name = $row['name']; @@ -130,17 +136,20 @@ <programlisting role="php"> <![CDATA[ <?php -$res = $db->query('SELECT id, name, email FROM users'); -$row = $res->fetchRow(DB_FETCHMODE_OBJECT); +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT id, name, email FROM users'); +$row =& $res->fetchRow(DB_FETCHMODE_OBJECT); + /* -$row will contain: -db_row Object -( - [id] => <column "id" data>, - [name] => <column "name" data>, - [email] => <column "email" data> -) -*/ + * $row will contain: + * db_row Object + * ( + * [id] => <column "id" data>, + * [name] => <column "name" data>, + * [email] => <column "email" data> + * ) + */ + // Access the data with: $id = $row->id; $name = $row->name; @@ -163,10 +172,14 @@ <title>Per call</title> <programlisting role="php"> <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT * FROM users'); -while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { +while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) { $id = $row['id']; } +?> ]]> </programlisting> </example> @@ -174,17 +187,16 @@ <title>Once per instance</title> <programlisting role="php"> <![CDATA[ -$db = DB::connect($dsn); -// this will set a default fetchmode for this Pear DB instance -// (for all queries) +<?php +// Once you have a valid DB object named $db... $db->setFetchMode(DB_FETCHMODE_ASSOC); -$result = $db->query(...); +$res =& $db->query('SELECT * FROM users'); -while ($row = $result->fetchRow()) { +while ($row =& $res->fetchRow()) { $id = $row['id']; } - +?> ]]> </programlisting> </example> @@ -203,7 +215,9 @@ <title>Fetching by number</title> <programlisting role="php"> <![CDATA[ -... +<?php +// Once you have a valid DB object named $db... + // the row to start fetching $from = 50; @@ -214,12 +228,13 @@ $to = $from + $resPage; foreach (range($from, $to) as $rowNum) { -if (!$row = $res->fetchrow($fetchmode, $rowNum)) { + if (!$row =& $res->fetchRow($fetchmode, $rowNum)) { break; } $id = $row[0]; .... } +?> ]]> </programlisting> </example> @@ -236,12 +251,14 @@ <title>Freeing</title> <programlisting role="php"> <![CDATA[ -... -$result = $db->query('SELECT * FROM clients'); -while ($row = $result->fetchRow()) { -... +<?php +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT name, address FROM clients'); +while ($row =& $res->fetchRow()) { + echo $row['name'] . ', ' . $row['address'] . "\n"; } -$result->free(); +$res->free(); +?> ]]> </programlisting> </example> @@ -261,7 +278,10 @@ first column from a query <programlisting role="php"> <![CDATA[ +<?php +// Once you have a valid DB object named $db... $numrows = $db->getOne('select count(id) from clients'); +?> ]]> </programlisting> </para> @@ -270,10 +290,13 @@ </link> returns the first row as an array. <programlisting role="php"> <![CDATA[ -$sql = 'select name, address, phone from clients where id=1'; -if (is_array($row = $db->getRow($sql))) { - list($name, $address, $phone) = $row; +<?php +// Once you have a valid DB object named $db... +$sql = 'select name, address from clients where id=1'; +while ($row =& $db->getRow($sql)) { + echo $row['name'] . ', ' . $row['address'] . "\n"; } +?> ]]> </programlisting> </para> @@ -284,13 +307,21 @@ as the second parameter. <programlisting role="php"> <![CDATA[ - $all_client_names = $db->getCol('SELECT name FROM clients'); +<?php +// Once you have a valid DB object named $db... +$data =& $db->getCol('SELECT name FROM clients'); + +/* + * Will return: + * array( + * 0 => 'Stig', + * 1 => 'Jon', + * 2 => 'Colin' + * ) + */ +?> ]]> </programlisting> - The above sentence could return for example: - <programlisting role="php"> - $all_client_names = array('Stig', 'Jon', 'Colin'); - </programlisting> </para> <para> <link linkend="package.database.db.db-common.getassoc"> @@ -299,15 +330,18 @@ first column as the key. <programlisting role="php"> <![CDATA[ -$data = getAssoc('SELECT name, surname, phone FROM mytable') +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT name, surname, phone FROM mytable'); /* -Will return: -array( - 'Peter' => array('Smith', '944679408'), - 'Tomas' => array('Cox', '944679408'), - 'Richard' => array('Merz', '944679408') -) -*/ + * Will return: + * array( + * 'Peter' => array('Smith', '944679408'), + * 'Tomas' => array('Cox', '944679408'), + * 'Richard' => array('Merz', '944679408') + * ) + */ +?> ]]> </programlisting> </para> @@ -317,15 +351,18 @@ fetches all the rows returned from a query- <programlisting role="php"> <![CDATA[ -$data = getAll('SELECT id, text, date FROM mytable'); +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAll('SELECT id, text, date FROM mytable'); /* -Will return: -array( - 1 => array('4', 'four', '2004'), - 2 => array('5', 'five', '2005'), - 3 => array('6', 'six', '2006') -) -*/ + * Will return: + * array( + * 0 => array('4', 'four', '2004'), + * 1 => array('5', 'five', '2005'), + * 2 => array('6', 'six', '2006') + * ) + */ +?> ]]> </programlisting> </para> @@ -351,8 +388,10 @@ rows returned from a "SELECT" query. <programlisting role="php"> <![CDATA[ +<?php // Number of rows echo $res->numRows(); +?> ]]> </programlisting> </para> @@ -364,8 +403,10 @@ columns returned from a "SELECT" query. <programlisting role="php"> <![CDATA[ +<?php // Number of cols echo $res->numCols(); +?> ]]> </programlisting> </para> @@ -378,9 +419,11 @@ "DELETE"). <programlisting role="php"> <![CDATA[ +<?php // remember that this statement won't return a result object $db->query('DELETE * FROM clients'); echo 'I have deleted ' . $db->affectedRows() . ' clients'; +?> ]]> </programlisting> </para> @@ -393,8 +436,10 @@ query. <programlisting role="php"> <![CDATA[ +<?php // Table Info print_r($res->tableInfo()); +?> ]]> </programlisting> </para>
http://cvs.php.net/diff.php/peardoc/en/package/database/db/intro-fetch.xml?r1=1.12&r2=1.13&ty=u Index: peardoc/en/package/database/db/intro-fetch.xml diff -u peardoc/en/package/database/db/intro-fetch.xml:1.12 peardoc/en/package/database/db/intro-fetch.xml:1.13 --- peardoc/en/package/database/db/intro-fetch.xml:1.12 Tue Jan 27 14:12:57 2004 +++ peardoc/en/package/database/db/intro-fetch.xml Sun Feb 1 23:26:03 2004 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.12 $ --> +<!-- $Revision: 1.13 $ --> <refentry id="package.database.db.intro-fetch"> <refnamediv> <refname>Introduction - Fetch & Get</refname> @@ -30,15 +30,15 @@ <programlisting role="php"> <![CDATA[ <?php -... -$db = DB::connect($dsn); -$res = $db->query("SELECT * FROM mytable"); +// Once you have a valid DB object named $db... +$res =& $db->query("SELECT * FROM mytable"); // Get each row of data on each iteration until // there are no more rows -while ($row = $res->fetchRow()) { +while ($row =& $res->fetchRow()) { $id = $row[0]; } + // Or: // an example using fetchInto() while ($res->fetchInto($row)) { @@ -66,16 +66,19 @@ <programlisting role="php"> <![CDATA[ <?php -$res = $db->query('SELECT id, name, email FROM users'); -$row = $res->fetchRow(DB_FETCHMODE_ORDERED); +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT id, name, email FROM users'); +$row =& $res->fetchRow(DB_FETCHMODE_ORDERED); + /* -$row will contain: -array ( - 0 => <column "id" data>, - 1 => <column "name" data>, - 2 => <column "email" data> -) -*/ + * $row will contain: + * array ( + * 0 => <column "id" data>, + * 1 => <column "name" data>, + * 2 => <column "email" data> + * ) + */ + // Access the data with: $id = $row[0]; $name = $row[1]; @@ -98,16 +101,19 @@ <programlisting role="php"> <![CDATA[ <?php -$res = $db->query('SELECT id, name, email FROM users'); -$row = $res->fetchRow(DB_FETCHMODE_ASSOC); +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT id, name, email FROM users'); +$row =& $res->fetchRow(DB_FETCHMODE_ASSOC); + /* -$row will contain: -array ( - 'id' => <column "id" data>, - 'name' => <column "name" data>, - 'email' => <column "email" data> -) -*/ + * $row will contain: + * array ( + * 'id' => <column "id" data>, + * 'name' => <column "name" data>, + * 'email' => <column "email" data> + * ) + */ + // Access the data with: $id = $row['id']; $name = $row['name']; @@ -130,17 +136,20 @@ <programlisting role="php"> <![CDATA[ <?php -$res = $db->query('SELECT id, name, email FROM users'); -$row = $res->fetchRow(DB_FETCHMODE_OBJECT); +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT id, name, email FROM users'); +$row =& $res->fetchRow(DB_FETCHMODE_OBJECT); + /* -$row will contain: -db_row Object -( - [id] => <column "id" data>, - [name] => <column "name" data>, - [email] => <column "email" data> -) -*/ + * $row will contain: + * db_row Object + * ( + * [id] => <column "id" data>, + * [name] => <column "name" data>, + * [email] => <column "email" data> + * ) + */ + // Access the data with: $id = $row->id; $name = $row->name; @@ -163,10 +172,14 @@ <title>Per call</title> <programlisting role="php"> <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT * FROM users'); -while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { +while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) { $id = $row['id']; } +?> ]]> </programlisting> </example> @@ -174,17 +187,16 @@ <title>Once per instance</title> <programlisting role="php"> <![CDATA[ -$db = DB::connect($dsn); -// this will set a default fetchmode for this Pear DB instance -// (for all queries) +<?php +// Once you have a valid DB object named $db... $db->setFetchMode(DB_FETCHMODE_ASSOC); -$result = $db->query(...); +$res =& $db->query('SELECT * FROM users'); -while ($row = $result->fetchRow()) { +while ($row =& $res->fetchRow()) { $id = $row['id']; } - +?> ]]> </programlisting> </example> @@ -203,7 +215,9 @@ <title>Fetching by number</title> <programlisting role="php"> <![CDATA[ -... +<?php +// Once you have a valid DB object named $db... + // the row to start fetching $from = 50; @@ -214,12 +228,13 @@ $to = $from + $resPage; foreach (range($from, $to) as $rowNum) { -if (!$row = $res->fetchrow($fetchmode, $rowNum)) { + if (!$row =& $res->fetchRow($fetchmode, $rowNum)) { break; } $id = $row[0]; .... } +?> ]]> </programlisting> </example> @@ -236,12 +251,14 @@ <title>Freeing</title> <programlisting role="php"> <![CDATA[ -... -$result = $db->query('SELECT * FROM clients'); -while ($row = $result->fetchRow()) { -... +<?php +// Once you have a valid DB object named $db... +$res =& $db->query('SELECT name, address FROM clients'); +while ($row =& $res->fetchRow()) { + echo $row['name'] . ', ' . $row['address'] . "\n"; } -$result->free(); +$res->free(); +?> ]]> </programlisting> </example> @@ -261,7 +278,10 @@ first column from a query <programlisting role="php"> <![CDATA[ +<?php +// Once you have a valid DB object named $db... $numrows = $db->getOne('select count(id) from clients'); +?> ]]> </programlisting> </para> @@ -270,10 +290,13 @@ </link> returns the first row as an array. <programlisting role="php"> <![CDATA[ -$sql = 'select name, address, phone from clients where id=1'; -if (is_array($row = $db->getRow($sql))) { - list($name, $address, $phone) = $row; +<?php +// Once you have a valid DB object named $db... +$sql = 'select name, address from clients where id=1'; +while ($row =& $db->getRow($sql)) { + echo $row['name'] . ', ' . $row['address'] . "\n"; } +?> ]]> </programlisting> </para> @@ -284,13 +307,21 @@ as the second parameter. <programlisting role="php"> <![CDATA[ - $all_client_names = $db->getCol('SELECT name FROM clients'); +<?php +// Once you have a valid DB object named $db... +$data =& $db->getCol('SELECT name FROM clients'); + +/* + * Will return: + * array( + * 0 => 'Stig', + * 1 => 'Jon', + * 2 => 'Colin' + * ) + */ +?> ]]> </programlisting> - The above sentence could return for example: - <programlisting role="php"> - $all_client_names = array('Stig', 'Jon', 'Colin'); - </programlisting> </para> <para> <link linkend="package.database.db.db-common.getassoc"> @@ -299,15 +330,18 @@ first column as the key. <programlisting role="php"> <![CDATA[ -$data = getAssoc('SELECT name, surname, phone FROM mytable') +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT name, surname, phone FROM mytable'); /* -Will return: -array( - 'Peter' => array('Smith', '944679408'), - 'Tomas' => array('Cox', '944679408'), - 'Richard' => array('Merz', '944679408') -) -*/ + * Will return: + * array( + * 'Peter' => array('Smith', '944679408'), + * 'Tomas' => array('Cox', '944679408'), + * 'Richard' => array('Merz', '944679408') + * ) + */ +?> ]]> </programlisting> </para> @@ -317,15 +351,18 @@ fetches all the rows returned from a query- <programlisting role="php"> <![CDATA[ -$data = getAll('SELECT id, text, date FROM mytable'); +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAll('SELECT id, text, date FROM mytable'); /* -Will return: -array( - 1 => array('4', 'four', '2004'), - 2 => array('5', 'five', '2005'), - 3 => array('6', 'six', '2006') -) -*/ + * Will return: + * array( + * 0 => array('4', 'four', '2004'), + * 1 => array('5', 'five', '2005'), + * 2 => array('6', 'six', '2006') + * ) + */ +?> ]]> </programlisting> </para> @@ -351,8 +388,10 @@ rows returned from a "SELECT" query. <programlisting role="php"> <![CDATA[ +<?php // Number of rows echo $res->numRows(); +?> ]]> </programlisting> </para> @@ -364,8 +403,10 @@ columns returned from a "SELECT" query. <programlisting role="php"> <![CDATA[ +<?php // Number of cols echo $res->numCols(); +?> ]]> </programlisting> </para> @@ -378,9 +419,11 @@ "DELETE"). <programlisting role="php"> <![CDATA[ +<?php // remember that this statement won't return a result object $db->query('DELETE * FROM clients'); echo 'I have deleted ' . $db->affectedRows() . ' clients'; +?> ]]> </programlisting> </para> @@ -393,8 +436,10 @@ query. <programlisting role="php"> <![CDATA[ +<?php // Table Info print_r($res->tableInfo()); +?> ]]> </programlisting> </para>