<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Send a query and fetch all results (if any)</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.dbx-fetch-row.html">dbx_fetch_row</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.dbx-sort.html">dbx_sort</a></div> <div class="up"><a href="ref.dbx.html">dbx Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.dbx-query" class="refentry"> <div class="refnamediv"> <h1 class="refname">dbx_query</h1> <p class="verinfo">(PHP 4 >= 4.0.6, PHP 5 < 5.1.0, PECL dbx >= 1.1.0)</p><p class="refpurpose"><span class="refname">dbx_query</span> — <span class="dc-title">Send a query and fetch all results (if any)</span></p> </div> <div class="refsect1 description" id="refsect1-function.dbx-query-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>dbx_query</strong></span> ( <span class="methodparam"><span class="type">object</span> <code class="parameter">$link_identifier</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$sql_statement</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span> ] )</div> <p class="para rdfs-comment"> Sends a query and fetch all results. </p> </div> <div class="refsect1 parameters" id="refsect1-function.dbx-query-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">link_identifier</code></dt> <dd> <p class="para"> The DBX link object returned by <span class="function"><a href="function.dbx-connect.html" class="function">dbx_connect()</a></span> </p> </dd> <dt> <code class="parameter">sql_statement</code></dt> <dd> <p class="para"> SQL statement. </p> <p class="para"> Data inside the query should be <a href="function.dbx-escape-string.html" class="link">properly escaped</a>. </p> </dd> <dt> <code class="parameter">flags</code></dt> <dd> <p class="para"> The <code class="parameter">flags</code> parameter is used to control the amount of information that is returned. It may be any combination of the following constants with the bitwise OR operator (|). The DBX_COLNAMES_* flags override the dbx.colnames_case setting from <var class="filename">php.ini</var>. <dl> <dt> <strong><code>DBX_RESULT_INDEX</code></strong> </dt> <dd> <span class="simpara"> It is <em class="emphasis">always</em> set, that is, the returned object has a <span class="property">data</span> property which is a 2 dimensional array indexed numerically. For example, in the expression <em>data[2][3]</em> <em>2</em> stands for the row (or record) number and <em>3</em> stands for the column (or field) number. The first row and column are indexed at 0. </span> <span class="simpara"> If <strong><code>DBX_RESULT_ASSOC</code></strong> is also specified, the returning object contains the information related to <strong><code>DBX_RESULT_INFO</code></strong> too, even if it was not specified. </span> </dd> <dt> <strong><code>DBX_RESULT_INFO</code></strong> </dt> <dd> <span class="simpara"> It provides info about columns, such as field names and field types. </span> </dd> <dt> <strong><code>DBX_RESULT_ASSOC</code></strong> </dt> <dd> <span class="simpara"> It effects that the field values can be accessed with the respective column names used as keys to the returned object's <span class="property">data</span> property. </span> <span class="simpara"> Associated results are actually references to the numerically indexed data, so modifying <em>data[0][0]</em> causes that <em>data[0]['field_name_for_first_column']</em> is modified as well. </span> </dd> <dt> <strong><code>DBX_RESULT_UNBUFFERED</code></strong> </dt> <dd> <span class="simpara"> This flag will not create the <span class="property">data</span> property, and the <span class="property">rows</span> property will initially be 0. Use this flag for large datasets, and use <span class="function"><a href="function.dbx-fetch-row.html" class="function">dbx_fetch_row()</a></span> to retrieve the results row by row. </span> <span class="simpara"> The <span class="function"><a href="function.dbx-fetch-row.html" class="function">dbx_fetch_row()</a></span> function will return rows that are conformant to the flags set with this query. Incidentally, it will also update the <span class="property">rows</span> each time it is called. </span> </dd> <dt> <strong><code>DBX_COLNAMES_UNCHANGED</code></strong> </dt> <dd> <span class="simpara"> The case of the returned column names will not be changed. </span> </dd> <dt> <strong><code>DBX_COLNAMES_UPPERCASE</code></strong> </dt> <dd> <span class="simpara"> The case of the returned column names will be changed to uppercase. </span> </dd> <dt> <strong><code>DBX_COLNAMES_LOWERCASE</code></strong> </dt> <dd> <span class="simpara"> The case of the returned column names will be changed to lowercase. </span> </dd> </dl> Note that <strong><code>DBX_RESULT_INDEX</code></strong> is always used, regardless of the actual value of <code class="parameter">flags</code> parameter. This means that only the following combinations are effective: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <strong><code>DBX_RESULT_INDEX</code></strong> </span> </li> <li class="listitem"> <span class="simpara"> <strong><code>DBX_RESULT_INDEX</code></strong> | <strong><code>DBX_RESULT_INFO</code></strong> </span> </li> <li class="listitem"> <span class="simpara"> <strong><code>DBX_RESULT_INDEX</code></strong> | <strong><code>DBX_RESULT_INFO</code></strong> | <strong><code>DBX_RESULT_ASSOC</code></strong> - this is the default, if <code class="parameter">flags</code> is not specified. </span> </li> </ul> </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.dbx-query-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>dbx_query()</strong></span> returns an object or <em>1</em> on success, and <em>0</em> on failure. The result object is returned only if the query given in <code class="parameter">sql_statement</code> produces a result set (i.e. a SELECT query, even if the result set is empty). </p> <p class="para"> The returned object has four or five properties depending on <code class="parameter">flags</code>: <dl> <dt> <span class="property">handle</span> </dt> <dd> <p class="para"> It is a valid handle for the connected database, and as such it can be used in module specific functions (if required). <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT id FROM table"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_field_len</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">handle</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </dd> <dt> <span class="property">cols</span> and <span class="property">rows</span> </dt> <dd> <p class="para"> These contain the number of columns (or fields) and rows (or records) respectively. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT id FROM table'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">rows</span><span style="color: #007700">; </span><span style="color: #FF8000">// number of records<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">cols</span><span style="color: #007700">; </span><span style="color: #FF8000">// number of fields <br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </dd> <dt> <span class="property">info</span> (optional) </dt> <dd> <span class="simpara"> It is returned only if either <strong><code>DBX_RESULT_INFO</code></strong> or <strong><code>DBX_RESULT_ASSOC</code></strong> is specified in the <code class="parameter">flags</code> parameter. It is a 2 dimensional array, that has two named rows (<em>name</em> and <em>type</em>) to retrieve column information. </span> <div class="example" id="example-1007"> <p><strong>Example #1 lists each field's name and type</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT id FROM table'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">DBX_RESULT_INDEX </span><span style="color: #007700">| </span><span style="color: #0000BB">DBX_RESULT_INFO</span><span style="color: #007700">);<br /><br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">cols</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++ ) {<br /> echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">info</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">][</span><span style="color: #0000BB">$i</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">info</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">][</span><span style="color: #0000BB">$i</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; <br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </dd> <dt> <span class="property">data</span> </dt> <dd> <span class="simpara"> This property contains the actual resulting data, possibly associated with column names as well depending on <code class="parameter">flags</code>. If <strong><code>DBX_RESULT_ASSOC</code></strong> is set, it is possible to use <em>$result->data[2]["field_name"]</em>. </span> <div class="example" id="example-1008"> <p><strong>Example #2 outputs the content of data property into HTML table</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT id, parentid, description FROM table'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"<table>\n"</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">data </span><span style="color: #007700">as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"<tr>\n"</span><span style="color: #007700">;<br /> foreach (</span><span style="color: #0000BB">$row </span><span style="color: #007700">as </span><span style="color: #0000BB">$field</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"<td></span><span style="color: #0000BB">$field</span><span style="color: #DD0000"></td>"</span><span style="color: #007700">;<br /> }<br /> echo </span><span style="color: #DD0000">"</tr>\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"</table>\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-1009"> <p><strong>Example #3 How to handle UNBUFFERED queries</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$result </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_query </span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT id, parentid, description FROM table'</span><span style="color: #007700">, </span><span style="color: #0000BB">DBX_RESULT_UNBUFFERED</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"<table>\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"<tr>\n"</span><span style="color: #007700">;<br /> foreach (</span><span style="color: #0000BB">$row </span><span style="color: #007700">as </span><span style="color: #0000BB">$field</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"<td></span><span style="color: #0000BB">$field</span><span style="color: #DD0000"></td>"</span><span style="color: #007700">;<br /> }<br /> echo </span><span style="color: #DD0000">"</tr>\n"</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"</table>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </dd> </dl> </p> </div> <div class="refsect1 changelog" id="refsect1-function.dbx-query-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.0.0</td> <td> Introduced <strong><code>DBX_RESULT_UNBUFFERED</code></strong>. </td> </tr> <tr> <td>4.3.0</td> <td> Introduced <strong><code>DBX_COLNAMES_UNCHANGED</code></strong>, <strong><code>DBX_COLNAMES_UPPERCASE</code></strong>, and <strong><code>DBX_COLNAMES_LOWERCASE</code></strong>. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.dbx-query-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1010"> <p><strong>Example #4 How to handle the returned value</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$link </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">DBX_ODBC</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"db"</span><span style="color: #007700">, </span><span style="color: #DD0000">"username"</span><span style="color: #007700">, </span><span style="color: #DD0000">"password"</span><span style="color: #007700">)<br /> or die(</span><span style="color: #DD0000">"Could not connect"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">dbx_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT id, parentid, description FROM table'</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">is_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">) ) {<br /> </span><span style="color: #FF8000">// ... do some stuff here, see detailed examples below ...<br /> // first, print out field names and types <br /> // then, draw a table filled with the returned field values<br /></span><span style="color: #007700">} else {<br /> exit(</span><span style="color: #DD0000">"Query failed"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">dbx_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.dbx-query-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Always refer to the module-specific documentation as well. </p> <p class="para"> Column names for queries on an Oracle database are returned in lowercase. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.dbx-query-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.dbx-escape-string.html" class="function" rel="rdfs-seeAlso">dbx_escape_string()</a> - Escape a string so it can safely be used in an sql-statement</span></li> <li class="member"><span class="function"><a href="function.dbx-fetch-row.html" class="function" rel="rdfs-seeAlso">dbx_fetch_row()</a> - Fetches rows from a query-result that had the DBX_RESULT_UNBUFFERED flag set</span></li> <li class="member"><span class="function"><a href="function.dbx-connect.html" class="function" rel="rdfs-seeAlso">dbx_connect()</a> - Open a connection/database</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.dbx-fetch-row.html">dbx_fetch_row</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.dbx-sort.html">dbx_sort</a></div> <div class="up"><a href="ref.dbx.html">dbx Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>