cvs: peardoc /en/core db.xml
| From: | Thomas V.V.Cox | Date: | Tue, 25 Dec 2001 15:45:41 +0000 |
| Subject: | cvs: peardoc /en/core db.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-1681@lists.php.net to get a copy of this message | ||
cox Tue Dec 25 10:45:41 2001 EDT
Modified files:
/peardoc/en/core db.xml
Log:
minor corrections to the Fetch chapter of Pear DB
Index: peardoc/en/core/db.xml diff -u peardoc/en/core/db.xml:1.6 peardoc/en/core/db.xml:1.7 --- peardoc/en/core/db.xml:1.6 Tue Dec 18 19:19:56 2001 +++ peardoc/en/core/db.xml Tue Dec 25 10:45:41 2001 @@ -1,5 +1,5 @@ <?xml encoding="iso-8859-1"?> -<!-- $Revision: 1.6 $ --> +<!-- $Revision: 1.7 $ --> <reference id="core.db"> <title>PEAR DB: a unified API for accessing SQL-databases</title> @@ -31,35 +31,35 @@ <member> <parameter>dbsyntax</parameter>: Database used with regards to SQL syntax etc. - </member> + </member> <member> <parameter>protocol</parameter>: Communication protocol to use ( i.e. tcp, unix etc.) - </member> + </member> <member> <parameter>hostspec</parameter>: Host specification (hostname[:port]) - </member> + </member> <member> <parameter>database</parameter>: Database to use on the DBMS server - </member> + </member> <member> <parameter>username</parameter>: User name for login - </member> + </member> <member> <parameter>password</parameter>: Password for login - </member> + </member> </simplelist> - </para> + </para> <para> The format of the supplied DSN is in its fullest form: <programlisting role="php"> -phptype(dbsyntax)://username:password@protocol+hostspec/database +phptype(dbsyntax)://username:password@protocol+hostspec/database </programlisting> - + Most variations are allowed: <programlisting role="php"> @@ -68,7 +68,7 @@ phptype://username:password@hostspec phptype://username@hostspec phptype://hostspec/database -phptype://hostspec +phptype://hostspec phptype(dbsyntax) phptype </programlisting> @@ -112,7 +112,7 @@ <title>Description</title> <simpara> To connect to a database you have to use the function - <function>DB::connect</function>, which requires a valid + <function>DB::connect</function>, which requires a valid <link linkend="packages.db.dsn">DSN</link> as parameter and optional a boolean value, which determines wether to use a persistent connection or not. In case of success you get a new @@ -154,7 +154,7 @@ ?> ]]> </programlisting> - </para> + </para> </refsect1> </refentry> <refentry id="core.db.query"> @@ -170,7 +170,7 @@ as an argument. On failure you get a DB Error object, check it with <function>DB::isError</function>. On succes you get <parameter>DB_OK</parameter> (predefined PEAR::DB constant) or - when you set a <parameter>SELECT</parameter>-statment a DB Result + when you set a <parameter>SELECT</parameter>-statment a DB Result object. </simpara> <para> @@ -190,7 +190,7 @@ ?> ]]> </programlisting> - </para> + </para> </refsect1> </refentry> @@ -236,7 +236,7 @@ </programlisting> </para> </refsect2> - + <refsect2> <title>Select the format of the fetched row</title> <para> @@ -254,11 +254,11 @@ <?php $res = $db->query('select id, name, email from users'); $row = $res->fetchRow($mode); -$row = array ( +$row = array ( 0 => <column "id" data>, 1 => <column "name" data>, - 2 => <column "email" data>); -?> + 2 => <column "email" data>); +?> ]]> </programlisting> </para> @@ -268,42 +268,42 @@ <parameter>DB_FETCHMODE_ASSOC</parameter> </para> <para> - Returns an associative array with column + Returns an associative array with column names as array keys <programlisting role="php"> <![CDATA[ <?php $res = $db->query('select id, name, email from users'); $row = $res->fetchRow($mode); -$row = array ( +$row = array ( 0 => <column "id" data>, 1 => <column "name" data>, - 2 => <column "email" data>); -?> + 2 => <column "email" data>); +?> ]]> - </programlisting> + </programlisting> </para> - </listitem> + </listitem> <listitem> <para> <parameter>DB_FETCHMODE_OBJECT</parameter> </para> <para> - Returns a DB_row object with column names + Returns a DB_row object with column names as properties <programlisting role="php"> <![CDATA[ -<?php -$row = db_row Object +<?php +$row = db_row Object ( [id] => <column "id" data>, - [name] => <column "name" data>, + [name] => <column "name" data>, [email] => <column "email" data> ) $id = $row->id; ?> ]]> - </programlisting> + </programlisting> </para> </listitem> </itemizedlist> @@ -314,7 +314,7 @@ <para> You can set the fetch mode for every call or for your whole DB instance. - + <programlisting role="php"> <![CDATA[ <?php @@ -340,21 +340,21 @@ while ($row = $result->fetchRow()) { $id = $row['id']; } -?> +?> ]]> - </programlisting> - </para> + </programlisting> + </para> </refsect2> <refsect2> <title>Fetch rows by number</title> - + <para> The PEAR DB fetch system also supports an extra parameter to the fetch statement. So you can fetch rows from a result - by number. This is especially helpful if you only want to show - sets of an entire result (for example in building paginated + by number. This is especially helpful if you only want to show + sets of an entire result (for example in building paginated HTML lists), fetch rows in an special order, etc. - + <programlisting role="php"> <![CDATA[ <?php @@ -369,17 +369,17 @@ $to = $from + $res_per_page; foreach (range($from, $to) as $rownum) { - if (!$row = $res->fetchrow($fetchmode, $rownum)) { - break; + if (!$row = $res->fetchrow($fetchmode, $rownum)) { + break; } - $id = $row[0]; + $id = $row[0]; .... } -?> +?> ]]> </programlisting> </para> - </refsect2> + </refsect2> <refsect2> <title>Freeing the result set</title> <para> @@ -388,36 +388,38 @@ Use <function>free</function> to do this. <programlisting role="php"> <![CDATA[ -<?php...$result = $db->query('SELECT * FROM clients'); +<?php +... +$result = $db->query('SELECT * FROM clients'); while ($row = $result->fetchRow()) { - ... +... } $result->free(); -?> +?> ]]> </programlisting> </para> </refsect2> <refsect2> <title>Quick data retrieving</title> - + <para> PEAR DB provides some special ways to retrieve information from a query without the need of using <function>fetch*</function> and loop throw results. </para> <para> - <function>getOne</function> retrieves the first result of the + <function>getOne</function> retrieves the first result of the first column from a query <programlisting role="php"> <![CDATA[ -$numrows = $db->getOne('select count(id) from clients'); +$numrows = $db->getOne('select count(id) from clients'); ]]> </programlisting> </para> <para> <function>getRow</function> will fetch the first row and return - it as an array + it as an array <programlisting role="php"> <![CDATA[ $sql = 'select name, address, phone from clients where id=1'; @@ -426,7 +428,7 @@ } ]]> </programlisting> - </para> + </para> <para> <function>getCol</function> will return an array with the data of the selected column. It accepts the column number to retrieve @@ -436,8 +438,8 @@ $all_client_names = $db->getCol('select name from clients'); ]]> </programlisting> - The next sentence could return for example: - $all_client_names = array('Stig', 'Jon', 'Colin'); + The above sentence could return for example: + $all_client_names = array('Stig', 'Jon', 'Colin'); </para> <para> The <function>get*</function> family methods will do all the @@ -445,11 +447,11 @@ and free the result. Please note that as all PEAR DB functions they will return a PEAR <classname>DB_error</classname> object on errors. - </para> + </para> </refsect2> <refsect2> <title>Getting more info from query results</title> - + <para> With Pear DB you have many ways to retrieve useful information from query results. These are: @@ -461,33 +463,33 @@ <programlisting role="php"> <![CDATA[ // Number of rows -echo $res->numRows(); +echo $res->numRows(); ]]> - </programlisting> - </para> + </programlisting> + </para> </listitem> <listitem> - <para> + <para> <function>numCols</function>: Returns the total number of columns returned from a "SELECT" query. - <programlisting role="php"> + <programlisting role="php"> <![CDATA[ // Number of cols -echo $res->numCols(); +echo $res->numCols(); ]]> </programlisting> - </para> + </para> </listitem> <listitem> - <para> - <function>affectedRows</function>: Returns the number of rows + <para> + <function>affectedRows</function>: Returns the number of rows affected by a data manipulation query ("INSERT", "UPDATE" or - "DELETE"). - <programlisting role="php"> + "DELETE"). + <programlisting role="php"> <![CDATA[ // remember that this statement won't return a result object -$db->query($sql); -echo 'I have deleted ' . $db->affectedRows() . 'clients'; +$db->query('DELETE * FROM clients'); +echo 'I have deleted ' . $db->affectedRows() . 'clients'; ]]> </programlisting> </para> @@ -504,7 +506,7 @@ ]]> </programlisting> </para> - </listitem> + </listitem> </itemizedlist> Don't forget to check if the returned result from your action is a PEAR Error object. If you get a error message like @@ -512,7 +514,7 @@ database backend doesn't support this action. </para> </refsect2> - </refsect1> + </refsect1> </refentry> <refentry id="core.db.sequences"> @@ -540,7 +542,7 @@ // Use the ID in your INSERT query $res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')"); ... -?> +?> ]]> </programlisting> </para> @@ -553,7 +555,7 @@ </refnamediv> <refsect1> <title>Description</title> - + <refsect2> <title>Purpose</title> <para> @@ -562,7 +564,7 @@ can use them, if you have to do more then one equal queries (i.e. adding a list of adresses to a database) or if you want to support different databases, which have different - implementations of the SQL standard. + implementations of the SQL standard. </para> <para> Maybe you want to support two databases with different INSERT @@ -580,16 +582,16 @@ $statment['db1']['INSERT_PERSON'] = "INSERT INTO person ( surname, name, age ) VALUES ( ?, ?, ? )" ; $statment['db2']['INSERT_PERSON'] = "INSERT INTO person SET surname=?, name=?, age=?" ; ]]> - </programlisting> + </programlisting> </para> </refsect2> - + <refsect2> <title>Prepare</title> <para> To use the features give in <link linkend="packages.db.prep_exec.purpose">Purpose</link> - you have to to two steps. Step one is to + you have to to two steps. Step one is to <emphasis>prepare</emphasis> the statment and the second is to <emphasis>excute</emphasis> it. </para> @@ -611,8 +613,8 @@ <![CDATA[ SELECT surname, name, age FROM person WHERE name = ? AND age < ? ]]> - </programlisting> - So, thats all! Now you have a generic statment, required by + </programlisting> + So, thats all! Now you have a generic statment, required by <function>prepare</function>. </para> <para> @@ -643,7 +645,7 @@ this, <function>execute</function> requires to arguments, the statment handle of <function>prepare</function> and a array with the values to assign. The array has to be numeric ordered. - The first entry of the array represents the first wildcard, + The first entry of the array represents the first wildcard, the second the second etc. The order is independent from the used wildcard char. <programlisting role="php"> @@ -652,14 +654,14 @@ //// Example inserting data $alldata = array( array(1, 'one', 'en'), array(2, 'two', 'to'), - array(3, 'three', 'tre'), + array(3, 'three', 'tre'), array(4, 'four', 'fire')); $sth = $dbh->prepare("INSERT INTO numbers VALUES(?,?,?)"); foreach ($alldata as $row) { $dbh->execute($sth, $row); } -?> - ]]> +?> + ]]> </programlisting> In the example the query is done four times: <programlisting role="php"> @@ -668,7 +670,7 @@ INSERT INTO numbers VALUES( '2', 'two', 'to') INSERT INTO numbers VALUES( '3', 'three', 'tre') INSERT INTO numbers VALUES( '4', 'four', 'fire') - ]]> + ]]> </programlisting> <function>ExecuteMultiple</function> works in the same way, but requires a two dimensional array. So you can avoid the explicit @@ -679,14 +681,14 @@ //// Example inserting data $alldata = array( array(1, 'one', 'en'), array(2, 'two', 'to'), - array(3, 'three', 'tre'), + array(3, 'three', 'tre'), array(4, 'four', 'fire')); $sth = $dbh->prepare("INSERT INTO numbers VALUES(?,?,?)"); $dbh->executeMultiple($sth, $alldata); } -?> - ]]> - </programlisting> +?> + ]]> + </programlisting> The result is the same. If one of the records failed, the unfinished records will not be executed. </para>
Index: peardoc/en/core/db.xml diff -u peardoc/en/core/db.xml:1.6 peardoc/en/core/db.xml:1.7 --- peardoc/en/core/db.xml:1.6 Tue Dec 18 19:19:56 2001 +++ peardoc/en/core/db.xml Tue Dec 25 10:45:41 2001 @@ -1,5 +1,5 @@ <?xml encoding="iso-8859-1"?> -<!-- $Revision: 1.6 $ --> +<!-- $Revision: 1.7 $ --> <reference id="core.db"> <title>PEAR DB: a unified API for accessing SQL-databases</title> @@ -31,35 +31,35 @@ <member> <parameter>dbsyntax</parameter>: Database used with regards to SQL syntax etc. - </member> + </member> <member> <parameter>protocol</parameter>: Communication protocol to use ( i.e. tcp, unix etc.) - </member> + </member> <member> <parameter>hostspec</parameter>: Host specification (hostname[:port]) - </member> + </member> <member> <parameter>database</parameter>: Database to use on the DBMS server - </member> + </member> <member> <parameter>username</parameter>: User name for login - </member> + </member> <member> <parameter>password</parameter>: Password for login - </member> + </member> </simplelist> - </para> + </para> <para> The format of the supplied DSN is in its fullest form: <programlisting role="php"> -phptype(dbsyntax)://username:password@protocol+hostspec/database +phptype(dbsyntax)://username:password@protocol+hostspec/database </programlisting> - + Most variations are allowed: <programlisting role="php"> @@ -68,7 +68,7 @@ phptype://username:password@hostspec phptype://username@hostspec phptype://hostspec/database -phptype://hostspec +phptype://hostspec phptype(dbsyntax) phptype </programlisting> @@ -112,7 +112,7 @@ <title>Description</title> <simpara> To connect to a database you have to use the function - <function>DB::connect</function>, which requires a valid + <function>DB::connect</function>, which requires a valid <link linkend="packages.db.dsn">DSN</link> as parameter and optional a boolean value, which determines wether to use a persistent connection or not. In case of success you get a new @@ -154,7 +154,7 @@ ?> ]]> </programlisting> - </para> + </para> </refsect1> </refentry> <refentry id="core.db.query"> @@ -170,7 +170,7 @@ as an argument. On failure you get a DB Error object, check it with <function>DB::isError</function>. On succes you get <parameter>DB_OK</parameter> (predefined PEAR::DB constant) or - when you set a <parameter>SELECT</parameter>-statment a DB Result + when you set a <parameter>SELECT</parameter>-statment a DB Result object. </simpara> <para> @@ -190,7 +190,7 @@ ?> ]]> </programlisting> - </para> + </para> </refsect1> </refentry> @@ -236,7 +236,7 @@ </programlisting> </para> </refsect2> - + <refsect2> <title>Select the format of the fetched row</title> <para> @@ -254,11 +254,11 @@ <?php $res = $db->query('select id, name, email from users'); $row = $res->fetchRow($mode); -$row = array ( +$row = array ( 0 => <column "id" data>, 1 => <column "name" data>, - 2 => <column "email" data>); -?> + 2 => <column "email" data>); +?> ]]> </programlisting> </para> @@ -268,42 +268,42 @@ <parameter>DB_FETCHMODE_ASSOC</parameter> </para> <para> - Returns an associative array with column + Returns an associative array with column names as array keys <programlisting role="php"> <![CDATA[ <?php $res = $db->query('select id, name, email from users'); $row = $res->fetchRow($mode); -$row = array ( +$row = array ( 0 => <column "id" data>, 1 => <column "name" data>, - 2 => <column "email" data>); -?> + 2 => <column "email" data>); +?> ]]> - </programlisting> + </programlisting> </para> - </listitem> + </listitem> <listitem> <para> <parameter>DB_FETCHMODE_OBJECT</parameter> </para> <para> - Returns a DB_row object with column names + Returns a DB_row object with column names as properties <programlisting role="php"> <![CDATA[ -<?php -$row = db_row Object +<?php +$row = db_row Object ( [id] => <column "id" data>, - [name] => <column "name" data>, + [name] => <column "name" data>, [email] => <column "email" data> ) $id = $row->id; ?> ]]> - </programlisting> + </programlisting> </para> </listitem> </itemizedlist> @@ -314,7 +314,7 @@ <para> You can set the fetch mode for every call or for your whole DB instance. - + <programlisting role="php"> <![CDATA[ <?php @@ -340,21 +340,21 @@ while ($row = $result->fetchRow()) { $id = $row['id']; } -?> +?> ]]> - </programlisting> - </para> + </programlisting> + </para> </refsect2> <refsect2> <title>Fetch rows by number</title> - + <para> The PEAR DB fetch system also supports an extra parameter to the fetch statement. So you can fetch rows from a result - by number. This is especially helpful if you only want to show - sets of an entire result (for example in building paginated + by number. This is especially helpful if you only want to show + sets of an entire result (for example in building paginated HTML lists), fetch rows in an special order, etc. - + <programlisting role="php"> <![CDATA[ <?php @@ -369,17 +369,17 @@ $to = $from + $res_per_page; foreach (range($from, $to) as $rownum) { - if (!$row = $res->fetchrow($fetchmode, $rownum)) { - break; + if (!$row = $res->fetchrow($fetchmode, $rownum)) { + break; } - $id = $row[0]; + $id = $row[0]; .... } -?> +?> ]]> </programlisting> </para> - </refsect2> + </refsect2> <refsect2> <title>Freeing the result set</title> <para> @@ -388,36 +388,38 @@ Use <function>free</function> to do this. <programlisting role="php"> <![CDATA[ -<?php...$result = $db->query('SELECT * FROM clients'); +<?php +... +$result = $db->query('SELECT * FROM clients'); while ($row = $result->fetchRow()) { - ... +... } $result->free(); -?> +?> ]]> </programlisting> </para> </refsect2> <refsect2> <title>Quick data retrieving</title> - + <para> PEAR DB provides some special ways to retrieve information from a query without the need of using <function>fetch*</function> and loop throw results. </para> <para> - <function>getOne</function> retrieves the first result of the + <function>getOne</function> retrieves the first result of the first column from a query <programlisting role="php"> <![CDATA[ -$numrows = $db->getOne('select count(id) from clients'); +$numrows = $db->getOne('select count(id) from clients'); ]]> </programlisting> </para> <para> <function>getRow</function> will fetch the first row and return - it as an array + it as an array <programlisting role="php"> <![CDATA[ $sql = 'select name, address, phone from clients where id=1'; @@ -426,7 +428,7 @@ } ]]> </programlisting> - </para> + </para> <para> <function>getCol</function> will return an array with the data of the selected column. It accepts the column number to retrieve @@ -436,8 +438,8 @@ $all_client_names = $db->getCol('select name from clients'); ]]> </programlisting> - The next sentence could return for example: - $all_client_names = array('Stig', 'Jon', 'Colin'); + The above sentence could return for example: + $all_client_names = array('Stig', 'Jon', 'Colin'); </para> <para> The <function>get*</function> family methods will do all the @@ -445,11 +447,11 @@ and free the result. Please note that as all PEAR DB functions they will return a PEAR <classname>DB_error</classname> object on errors. - </para> + </para> </refsect2> <refsect2> <title>Getting more info from query results</title> - + <para> With Pear DB you have many ways to retrieve useful information from query results. These are: @@ -461,33 +463,33 @@ <programlisting role="php"> <![CDATA[ // Number of rows -echo $res->numRows(); +echo $res->numRows(); ]]> - </programlisting> - </para> + </programlisting> + </para> </listitem> <listitem> - <para> + <para> <function>numCols</function>: Returns the total number of columns returned from a "SELECT" query. - <programlisting role="php"> + <programlisting role="php"> <![CDATA[ // Number of cols -echo $res->numCols(); +echo $res->numCols(); ]]> </programlisting> - </para> + </para> </listitem> <listitem> - <para> - <function>affectedRows</function>: Returns the number of rows + <para> + <function>affectedRows</function>: Returns the number of rows affected by a data manipulation query ("INSERT", "UPDATE" or - "DELETE"). - <programlisting role="php"> + "DELETE"). + <programlisting role="php"> <![CDATA[ // remember that this statement won't return a result object -$db->query($sql); -echo 'I have deleted ' . $db->affectedRows() . 'clients'; +$db->query('DELETE * FROM clients'); +echo 'I have deleted ' . $db->affectedRows() . 'clients'; ]]> </programlisting> </para> @@ -504,7 +506,7 @@ ]]> </programlisting> </para> - </listitem> + </listitem> </itemizedlist> Don't forget to check if the returned result from your action is a PEAR Error object. If you get a error message like @@ -512,7 +514,7 @@ database backend doesn't support this action. </para> </refsect2> - </refsect1> + </refsect1> </refentry> <refentry id="core.db.sequences"> @@ -540,7 +542,7 @@ // Use the ID in your INSERT query $res = $db->query("INSERT INTO myTable (id,text) VALUES ($id,'foo')"); ... -?> +?> ]]> </programlisting> </para> @@ -553,7 +555,7 @@ </refnamediv> <refsect1> <title>Description</title> - + <refsect2> <title>Purpose</title> <para> @@ -562,7 +564,7 @@ can use them, if you have to do more then one equal queries (i.e. adding a list of adresses to a database) or if you want to support different databases, which have different - implementations of the SQL standard. + implementations of the SQL standard. </para> <para> Maybe you want to support two databases with different INSERT @@ -580,16 +582,16 @@ $statment['db1']['INSERT_PERSON'] = "INSERT INTO person ( surname, name, age ) VALUES ( ?, ?, ? )" ; $statment['db2']['INSERT_PERSON'] = "INSERT INTO person SET surname=?, name=?, age=?" ; ]]> - </programlisting> + </programlisting> </para> </refsect2> - + <refsect2> <title>Prepare</title> <para> To use the features give in <link linkend="packages.db.prep_exec.purpose">Purpose</link> - you have to to two steps. Step one is to + you have to to two steps. Step one is to <emphasis>prepare</emphasis> the statment and the second is to <emphasis>excute</emphasis> it. </para> @@ -611,8 +613,8 @@ <![CDATA[ SELECT surname, name, age FROM person WHERE name = ? AND age < ? ]]> - </programlisting> - So, thats all! Now you have a generic statment, required by + </programlisting> + So, thats all! Now you have a generic statment, required by <function>prepare</function>. </para> <para> @@ -643,7 +645,7 @@ this, <function>execute</function> requires to arguments, the statment handle of <function>prepare</function> and a array with the values to assign. The array has to be numeric ordered. - The first entry of the array represents the first wildcard, + The first entry of the array represents the first wildcard, the second the second etc. The order is independent from the used wildcard char. <programlisting role="php"> @@ -652,14 +654,14 @@ //// Example inserting data $alldata = array( array(1, 'one', 'en'), array(2, 'two', 'to'), - array(3, 'three', 'tre'), + array(3, 'three', 'tre'), array(4, 'four', 'fire')); $sth = $dbh->prepare("INSERT INTO numbers VALUES(?,?,?)"); foreach ($alldata as $row) { $dbh->execute($sth, $row); } -?> - ]]> +?> + ]]> </programlisting> In the example the query is done four times: <programlisting role="php"> @@ -668,7 +670,7 @@ INSERT INTO numbers VALUES( '2', 'two', 'to') INSERT INTO numbers VALUES( '3', 'three', 'tre') INSERT INTO numbers VALUES( '4', 'four', 'fire') - ]]> + ]]> </programlisting> <function>ExecuteMultiple</function> works in the same way, but requires a two dimensional array. So you can avoid the explicit @@ -679,14 +681,14 @@ //// Example inserting data $alldata = array( array(1, 'one', 'en'), array(2, 'two', 'to'), - array(3, 'three', 'tre'), + array(3, 'three', 'tre'), array(4, 'four', 'fire')); $sth = $dbh->prepare("INSERT INTO numbers VALUES(?,?,?)"); $dbh->executeMultiple($sth, $alldata); } -?> - ]]> - </programlisting> +?> + ]]> + </programlisting> The result is the same. If one of the records failed, the unfinished records will not be executed. </para>