cvs: peardoc /en/package/database/db/db-common getassoc.xml

From: Date: Thu, 15 Apr 2004 04:59:40 +0000
Subject: cvs: peardoc /en/package/database/db/db-common getassoc.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-3471@lists.php.net to get a copy of this message
danielc Thu Apr 15 00:59:40 2004 EDT Modified files: /peardoc/en/package/database/db/db-common getassoc.xml Log: Provide thorough examples.

http://cvs.php.net/diff.php/peardoc/en/package/database/db/db-common/getassoc.xml?r1=1.24&r2=1.25&ty=u Index: peardoc/en/package/database/db/db-common/getassoc.xml diff -u peardoc/en/package/database/db/db-common/getassoc.xml:1.24 peardoc/en/package/database/db/db-common/getassoc.xml:1.25 --- peardoc/en/package/database/db/db-common/getassoc.xml:1.24 Wed Apr 14 11:42:11 2004 +++ peardoc/en/package/database/db/db-common/getassoc.xml Thu Apr 15 00:59:40 2004 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.24 $ --> +<!-- $Revision: 1.25 $ --> <refentry id="package.database.db.db-common.getassoc"> <refnamediv> @@ -236,30 +236,696 @@ <refsect1 id="package.database.db.db-common.getassoc.example"> &title.example; - <example> - <title>Using <function></function></title> - <programlisting role="php"> + <para> + All of the examples use the following data set: + <programlisting role="sql"> <![CDATA[ +INSERT INTO foo VALUES ('Juan', 5, '1991-01-11 21:31:41'); +INSERT INTO foo VALUES ('Kyu', 10, '1992-02-12 22:32:42'); +INSERT INTO foo VALUES ('Kyu', 15, '1993-03-13 23:33:43'); + ]]> + </programlisting> + </para> + + <refsect2 id="package.database.db.db-common.getassoc.example.two"> + <title>Result sets having two columns</title> + <para> + When using <function>getAssoc</function> for results + which have two columns and + <parameter>$force_array</parameter> = &false; (the default) + changing <parameter>$fetchmode</parameter> has no + impact on the format of the resulting array. + </para> + + <example> + <title> + Using <function>getAssoc</function> in default mode + </title> + <programlisting role="php"> + <![CDATA[ <?php // Once you have a valid DB object named $db... -$data =& $db->getAssoc('SELECT name, surname, phone FROM mytable'); +$data =& $db->getAssoc('SELECT cf, df FROM foo'); if (DB::isError($data)) { die($data->getMessage()); } +print_r($data); + /* - * Returns: - * array( - * 'Peter' => array('Smith', '944679408'), - * 'Tomas' => array('Cox', '944679408'), - * 'Richard' => array('Merz', '944679408') + * Array + * ( + * [Juan] => 1991-01-11 21:31:41 + * [Kyu] => 1993-03-13 23:33:43 * ) */ ?> - ]]> - </programlisting> - </example> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + false, array(), DB_FETCHMODE_ORDERED, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => 1991-01-11 21:31:41 + * ) + * [Kyu] => Array + * ( + * [0] => 1992-02-12 22:32:42 + * [1] => 1993-03-13 23:33:43 + * ) + * ) + * + * The same result is produced by calling: + * $db->getAssoc('SELECT cf, df FROM foo', + * false, array(), DB_FETCHMODE_ASSOC, true); + * $db->getAssoc('SELECT cf, df FROM foo', + * false, array(), DB_FETCHMODE_OBJECT, true); + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$force_array</parameter> = &true; + and <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ORDERED</constant> + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + true, array(), DB_FETCHMODE_ORDERED); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => 1991-01-11 21:31:41 + * ) + * [Kyu] => Array + * ( + * [0] => 1993-03-13 23:33:43 + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$force_array</parameter> = &true; + and <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ASSOC</constant> + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + true, array(), DB_FETCHMODE_ASSOC); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [df] => 1991-01-11 21:31:41 + * ) + * [Kyu] => Array + * ( + * [df] => 1993-03-13 23:33:43 + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$force_array</parameter> = &true; + and <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_OBJECT</constant> + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + true, array(), DB_FETCHMODE_OBJECT); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => stdClass Object + * ( + * [cf] => Juan + * [df] => 1991-01-11 21:31:41 + * ) + * [Kyu] => stdClass Object + * ( + * [cf] => Kyu + * [df] => 1993-03-13 23:33:43 + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$force_array</parameter> = &true;, + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ORDERED</constant> + and <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + true, array(), DB_FETCHMODE_ORDERED, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => Array + * ( + * [0] => 1991-01-11 21:31:41 + * ) + * ) + * [Kyu] => Array + * ( + * [0] => Array + * ( + * [0] => 1992-02-12 22:32:42 + * ) + * [1] => Array + * ( + * [0] => 1993-03-13 23:33:43 + * ) + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$force_array</parameter> = &true;, + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ASSOC</constant> + and <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + true, array(), DB_FETCHMODE_ASSOC, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => Array + * ( + * [df] => 1991-01-11 21:31:41 + * ) + * + * ) + * [Kyu] => Array + * ( + * [0] => Array + * ( + * [df] => 1992-02-12 22:32:42 + * ) + * [1] => Array + * ( + * [df] => 1993-03-13 23:33:43 + * ) + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$force_array</parameter> = &true;, + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_OBJECT</constant> + and <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo', + true, array(), DB_FETCHMODE_OBJECT, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => stdClass Object + * ( + * [cf] => Juan + * [df] => 1991-01-11 21:31:41 + * ) + * ) + * [Kyu] => Array + * ( + * [0] => stdClass Object + * ( + * [cf] => Kyu + * [df] => 1992-02-12 22:32:42 + * ) + * [1] => stdClass Object + * ( + * [cf] => Kyu + * [df] => 1993-03-13 23:33:43 + * ) + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + </refsect2> + + + <refsect2 id="package.database.db.db-common.getassoc.example.more"> + <title>Result sets having more than two columns</title> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ORDERED</constant> + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, nf, df FROM foo', + false, array(), DB_FETCHMODE_ORDERED); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => 5 + * [1] => 1991-01-11 21:31:41 + * ) + * [Kyu] => Array + * ( + * [0] => 15 + * [1] => 1993-03-13 23:33:43 + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ASSOC</constant> + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, nf, df FROM foo', + false, array(), DB_FETCHMODE_ASSOC); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [nf] => 5 + * [df] => 1991-01-11 21:31:41 + * ) + * [Kyu] => Array + * ( + * [nf] => 15 + * [df] => 1993-03-13 23:33:43 + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_OBJECT</constant> + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, nf, df FROM foo', + false, array(), DB_FETCHMODE_OBJECT); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => stdClass Object + * ( + * [cf] => Juan + * [nf] => 5 + * [df] => 1991-01-11 21:31:41 + * ) + * [Kyu] => stdClass Object + * ( + * [cf] => Kyu + * [nf] => 15 + * [df] => 1993-03-13 23:33:43 + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ORDERED</constant> + and <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, nf, df FROM foo', + false, array(), DB_FETCHMODE_ORDERED, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => Array + * ( + * [0] => 5 + * [1] => 1991-01-11 21:31:41 + * ) + * ) + * [Kyu] => Array + * ( + * [0] => Array + * ( + * [0] => 10 + * [1] => 1992-02-12 22:32:42 + * ) + * [1] => Array + * ( + * [0] => 15 + * [1] => 1993-03-13 23:33:43 + * ) + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_ASSOC</constant> + and <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, nf, df FROM foo', + false, array(), DB_FETCHMODE_ASSOC, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => Array + * ( + * [nf] => 5 + * [df] => 1991-01-11 21:31:41 + * ) + * ) + * [Kyu] => Array + * ( + * [0] => Array + * ( + * [nf] => 10 + * [df] => 1992-02-12 22:32:42 + * ) + * [1] => Array + * ( + * [nf] => 15 + * [df] => 1993-03-13 23:33:43 + * ) + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> with + <parameter>$fetchmode</parameter> = + <constant>DB_FETCHMODE_OBJECT</constant> + and <parameter>$group</parameter> = &true; + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, nf, df FROM foo', + false, array(), DB_FETCHMODE_OBJECT, true); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +print_r($data); + +/* + * Array + * ( + * [Juan] => Array + * ( + * [0] => stdClass Object + * ( + * [cf] => Juan + * [nf] => 5 + * [df] => 1991-01-11 21:31:41 + * ) + * ) + * [Kyu] => Array + * ( + * [0] => stdClass Object + * ( + * [cf] => Kyu + * [nf] => 10 + * [df] => 1992-02-12 22:32:42 + * ) + * [1] => stdClass Object + * ( + * [cf] => Kyu + * [nf] => 15 + * [df] => 1993-03-13 23:33:43 + * ) + * ) + * ) + */ +?> + ]]> + </programlisting> + </example> + + </refsect2> + + <refsect2 id="package.database.db.db-common.getassoc.example.params"> + <title>Prepare / Execute Mode</title> + + <example> + <title> + Using <function>getAssoc</function> + with one placeholder + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo WHERE nf = ?', + false, 5); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +// ... +?> + ]]> + </programlisting> + </example> + + <example> + <title> + Using <function>getAssoc</function> + with two placeholders + </title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$data =& $db->getAssoc('SELECT cf, df FROM foo WHERE nf IN (?, ?)', + false, array(5, 10)); + +if (DB::isError($data)) { + die($data->getMessage()); +} + +// ... +?> + ]]> + </programlisting> + </example> + + </refsect2> </refsect1> <refsect1 id="package.database.db.db-common.getassoc.see">
« previous php.pear.doc (#3471) next »