📄 function.pg-fetch-object.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Fetch a row as an object</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-result.html">pg_fetch_result</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.pg-fetch-object" class="refentry"> <div class="refnamediv"> <h1 class="refname">pg_fetch_object</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">pg_fetch_object</span> — <span class="dc-title">Fetch a row as an object</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">object</span> <span class="methodname"><b><b>pg_fetch_object</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$result</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$row</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$result_type</tt></span> ]] )</div> <div class="methodsynopsis dc-description"> <span class="type">object</span> <span class="methodname"><b><b>pg_fetch_object</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$result</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$row</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$class_name</tt></span> [, <span class="methodparam"><span class="type">array</span> <tt class="parameter">$params</tt></span> ]]] )</div> <p class="para rdfs-comment"> <b>pg_fetch_object()</b> returns an object with properties that correspond to the fetched row's field names. It can optionally instantiate an object of a specific class, and pass parameters to that class's constructor. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara">This function sets NULL fields tothe PHP <b><tt>NULL</tt></b> value.</span></p></blockquote> <p class="para"> Speed-wise, the function is identical to <a href="function.pg-fetch-array.html" class="function">pg_fetch_array()</a>, and almost as fast as <a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a> (the difference is insignificant). </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">result</tt></i></span> <dd> <p class="para"> PostgreSQL query result resource, returned by <a href="function.pg-query.html" class="function">pg_query()</a>, <a href="function.pg-query-params.html" class="function">pg_query_params()</a> or <a href="function.pg-execute.html" class="function">pg_execute()</a> (among others). </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">row</tt></i></span> <dd> <p class="para"> Row number in result to fetch. Rows are numbered from 0 upwards. If omitted, next row is fetched. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">result_type</tt></i></span> <dd> <p class="para"> Ignored and deprecated. Defaults to <b><tt>PGSQL_ASSOC</tt></b>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">class_name</tt></i></span> <dd> <p class="para"> The name of the class to instantiate, set the properties of and return. If not specified, a <b class="classname">stdClass</b> object is returned. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">params</tt></i></span> <dd> <p class="para"> An optional <a href="language.types.array.html" class="type array">array</a> of parameters to pass to the constructor for <i><tt class="parameter">class_name</tt></i> objects. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> An <a href="language.types.object.html" class="type object">object</a> with one attribute for each field name in the result. Database <i>NULL</i> values are returned as <b><tt>NULL</tt></b>. </p> <p class="para"> <b><tt>FALSE</tt></b> is returned if <i><tt class="parameter">row</tt></i> exceeds the number of rows in the set, there are no more rows, or on any other error. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.0.0</td> <td colspan="1" rowspan="1" align="left"> <i><tt class="parameter">class_name</tt></i> and <i><tt class="parameter">params</tt></i> were added. The old form with <i><tt class="parameter">result_type</tt></i> still exists for backwards compatibility. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> <i><tt class="parameter">result_type</tt></i> default changed from <b><tt>PGSQL_BOTH</tt></b> to <b><tt>PGSQL_ASSOC</tt></b>, since the numeric index was illegal. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.1.0</td> <td colspan="1" rowspan="1" align="left"> The parameter <i><tt class="parameter">row</tt></i> became optional. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>pg_fetch_object()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php <br /><br />$database </span><span style="color: #007700">= </span><span style="color: #DD0000">"store"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$db_conn </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=localhost port=5432 dbname=$database"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$db_conn</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Failed connecting to postgres database $database\n"</span><span style="color: #007700">;<br /> exit;<br />}<br /><br /></span><span style="color: #0000BB">$qu </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT * FROM books ORDER BY author"</span><span style="color: #007700">);<br /><br /><br />while (</span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$qu</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #0000BB">$data</span><span style="color: #007700">-></span><span style="color: #0000BB">author </span><span style="color: #007700">. </span><span style="color: #DD0000">" ("</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$data</span><span style="color: #007700">-></span><span style="color: #0000BB">year </span><span style="color: #007700">. </span><span style="color: #DD0000">"): "</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$data</span><span style="color: #007700">-></span><span style="color: #0000BB">title </span><span style="color: #007700">. </span><span style="color: #DD0000">"<br />"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">pg_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$qu</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">pg_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.pg-query.html" class="function" rel="rdfs-seeAlso">pg_query()</a></li> <li class="member"><a href="function.pg-fetch-array.html" class="function" rel="rdfs-seeAlso">pg_fetch_array()</a></li> <li class="member"><a href="function.pg-fetch-assoc.html" class="function" rel="rdfs-seeAlso">pg_fetch_assoc()</a></li> <li class="member"><a href="function.pg-fetch-row.html" class="function" rel="rdfs-seeAlso">pg_fetch_row()</a></li> <li class="member"><a href="function.pg-fetch-result.html" class="function" rel="rdfs-seeAlso">pg_fetch_result()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-result.html">pg_fetch_result</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -