php-doc-en/reference/pgsql/functions/pg-fetch-object.xml
2003-02-21 23:17:32 +00:00

137 lines
4 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.9 $ -->
<!-- splitted from ./en/functions/pgsql.xml, last change in rev 1.2 -->
<refentry id="function.pg-fetch-object">
<refnamediv>
<refname>pg_fetch_object</refname>
<refpurpose>Fetch a row as an object</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>object</type><methodname>pg_fetch_object</methodname>
<methodparam><type>resource</type><parameter>result</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>row</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>result_type</parameter></methodparam>
</methodsynopsis>
<para>
<function>pg_fetch_object</function> returns an object with
properties that correspond to the fetched row. It returns &false;
if there are no more rows or error.
</para>
<para>
<function>pg_fetch_object</function> is similar to
<function>pg_fetch_array</function>, with one difference - an
object is returned, instead of an array. Indirectly, that means
that you can only access the data by the field names, and not by
their offsets (numbers are illegal property names).
</para>
<para>
<parameter>row</parameter> is row (record) number to be
retrieved. First row is 0.
</para>
<para>
Speed-wise, the function is identical to
<function>pg_fetch_array</function>, and almost as quick as
<function>pg_fetch_row</function> (the difference is
insignificant).
</para>
<note>
<para>
From 4.1.0, <parameter>row</parameter> is optional.
</para>
<para>
From 4.3.0, <parameter>result_type</parameter> is default to PGSQL_ASSOC while
older versions' default was PGSQL_BOTH. There is no use for numeric property,
since numeric property name is invalid in PHP.
</para>
<para>
<parameter>result_type</parameter> may be deleted in future versions.
</para>
</note>
<para>
<example>
<title>Postgres fetch object</title>
<programlisting role="php">
<![CDATA[
<?php
$database = "verlag";
$db_conn = pg_connect ("host=localhost port=5432 dbname=$database");
if (!$db_conn): ?>
<H1>Failed connecting to postgres database <?php echo $database ?></H1> <?php
exit;
endif;
$qu = pg_query ($db_conn, "SELECT * FROM verlag ORDER BY autor");
$row = 0; // postgres needs a row counter other dbs might not
while ($data = pg_fetch_object ($qu, $row)) {
echo $data->autor." (";
echo $data->jahr ."): ";
echo $data->titel."<BR>";
$row++;
}
?>
<PRE>
<?php
$fields[] = Array ("autor", "Author");
$fields[] = Array ("jahr", " Year");
$fields[] = Array ("titel", " Title");
$row= 0; // postgres needs a row counter other dbs might not
while ($data = pg_fetch_object ($qu, $row)) {
echo "----------\n";
reset ($fields);
while (list (,$item) = each ($fields)):
echo $item[1].": ".$data->$item[0]."\n";
endwhile;
$row++;
}
echo "----------\n";
?>
</PRE>
<?php
pg_free_result ($qu);
pg_close ($db_conn);
?>
]]>
</programlisting>
</example>
</para>
<para>
See also
<function>pg_query</function>,
<function>pg_fetch_array</function>,
<function>pg_fetch_row</function> and
<function>pg_fetch_result</function>.
</para>
<note>
<para>
From 4.1.0, <parameter>row</parameter> became optional.
Calling <function>pg_fetch_object</function> will increment
internal row counter counter by 1.
</para>
</note>
</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
-->