php-doc-en/reference/pgsql/functions/pg-fetch-object.xml

133 lines
4 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<!-- 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><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>result_type</parameter> is optional parameter controls
how return value is initialized.
<parameter>result_type</parameter> is a constant and can take the
following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH.
<function>pg_fetch_array</function> returns associative array
that has field name as key for PGSQL_ASSOC. field index as key
with PGSQL_NUM and both field name/index as key with
PGSQL_BOTH. Default is PGSQL_BOTH.
</para>
<para>
<note>
<para>
<parameter>result_type</parameter> was added in PHP 4.0.
</para>
</note>
</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>
<para>
See also <function>pg_query</function>, <function>pg_fetch_array</function>,
<function>pg_fetch_row</function> and <function>pg_fetch_result</function>.
</para>
<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>
<note>
<para>
From 4.1.0, <parameter>row</parameter> became optional.
</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
-->