php-doc-en/reference/pdo/functions/PDOStatement-fetch.xml

143 lines
4.1 KiB
XML

<?xml version='1.0' encoding='iso-8859-1'?>
<!-- $Revision: 1.3 $ -->
<!-- Generated by xml_proto.php v2.1. Found in /scripts directory of phpdoc. -->
<refentry id="function.PDOStatement-fetch">
<refnamediv>
<refname>PDOStatement::fetch</refname>
<refpurpose>
Fetches the next row from a result set
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>PDOStatement::fetch</methodname>
<methodparam choice="opt"><type>int</type><parameter>fetch_style</parameter></methodparam>
</methodsynopsis>
&warn.experimental.func;
<para>
Fetches a row from a result set associated with a PDOStatement object.
</para>
<para>
<parameter>fetch_style</parameter> can be one of the following values:
<itemizedlist>
<listitem><para>
<literal>PDO_FETCH_ASSOC</literal>: returns an array indexed by column
name as returned in your result set
</para></listitem>
<listitem><para>
<literal>PDO_FETCH_BOTH</literal> (default): returns an array indexed by
both column name and column number as returned in your result set
</para></listitem>
<listitem><para>
<literal>PDO_FETCH_BOUND</literal>: returns &true; and assigns the
values of the columns in your result set to the PHP variables to which
they were bound with the <function>PDO::bindParam</function> method
</para></listitem>
<listitem><para>
<literal>PDO_FETCH_LAZY</literal>: combines
<literal>PDO_FETCH_BOTH</literal> and <literal>PDO_FETCH_OBJ</literal>,
creating the object variable names as they are accessed
</para></listitem>
<listitem><para>
<literal>PDO_FETCH_OBJ</literal>: returns an anonymous object with
property names that correspond to the column names returned in your
result set
</para></listitem>
<listitem><para>
<literal>PDO_FETCH_NUM</literal>: returns an array indexed by column
number as returned in your result set, starting at column 0
</para></listitem>
</itemizedlist>
</para>
<example><title>Fetching rows using different fetch styles</title>
<programlisting role="php">
<![CDATA[
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Exercise PDOStatement::fetch styles */
print("PDO_FETCH_ASSOC: ");
print("Return next row as an array indexed by column name\n");
$result = $sth->fetch(PDO_FETCH_ASSOC);
print_r($result);
print("\n");
print("PDO_FETCH_BOTH: ");
print("Return next row as an array indexed by both column name and number\n");
$result = $sth->fetch(PDO_FETCH_BOTH);
print_r($result);
print("\n");
print("PDO_FETCH_LAZY: ");
print("Return next row as an anonymous object with column names as properties\n");
$result = $sth->fetch(PDO_FETCH_LAZY);
print_r($result);
print("\n");
print("PDO_FETCH_OBJ: ");
print("Return next row as an anonymous object with column names as properties\n");
$result = $sth->fetch(PDO_FETCH_OBJ);
print $result->NAME;
print("\n");
?>
]]>
</programlisting>
</example>
&example.outputs;
<screen>
<![CDATA[
PDO_FETCH_ASSOC: Return next row as an array indexed by column name
Array
(
[NAME] => apple
[COLOUR] => red
)
PDO_FETCH_BOTH: Return next row as an array indexed by both column name and number
Array
(
[NAME] => banana
[0] => banana
[COLOUR] => yellow
[1] => yellow
)
PDO_FETCH_LAZY: Return next row as an anonymous object with column names as properties
PDORow Object
(
[NAME] => orange
[COLOUR] => orange
)
PDO_FETCH_OBJ: Return next row as an anonymous object with column names as properties
kiwi
]]>
</screen>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->