<HTML ><HEAD ><TITLE >dbx_query</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="dbx functions" HREF="ref.dbx.html"><LINK REL="PREVIOUS" TITLE="dbx_escape_string" HREF="function.dbx-escape-string.html"><LINK REL="NEXT" TITLE="dbx_sort" HREF="function.dbx-sort.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.dbx-escape-string.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.dbx-sort.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.dbx-query" ></A >dbx_query</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN18040" ></A ><P > (PHP 4 >= 4.0.6)</P >dbx_query -- Send a query and fetch all results (if any)</DIV ><DIV CLASS="refsect1" ><A NAME="AEN18043" ></A ><H2 >Description</H2 >object <B CLASS="methodname" >dbx_query</B > ( object link_identifier, string sql_statement [, long flags])<BR ></BR ><P > <B CLASS="function" >dbx_query()</B > returns an object or <TT CLASS="literal" >1</TT > on success, and <TT CLASS="literal" >0</TT > on failure. The result object is returned only if the query given in <TT CLASS="parameter" ><I >sql_statement</I ></TT > produces a result set. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN18062" ></A ><P ><B >Example 1. How to handle the returned value</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $link = dbx_connect(DBX_ODBC, "", "db", "username", "password") or die("Could not connect"); $result = dbx_query($link, 'SELECT id, parentid, description FROM table'); if ( is_object($result) ) { // ... do some stuff here, see detailed examples below ... // first, print out field names and types // then, draw a table filled with the returned field values } else if ( $result == 1 ) { echo("Query executed successfully, but no result set returned"); } else { exit("Query failed"); } dbx_close($link); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > The <TT CLASS="parameter" ><I >flags</I ></TT > 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 <TT CLASS="filename" >php.ini</TT >. <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >DBX_RESULT_INDEX</B ></TT ></DT ><DD ><P > It is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >always</I ></SPAN > 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 <TT CLASS="literal" >data[2][3]</TT > <TT CLASS="literal" >2</TT > stands for the row (or record) number and <TT CLASS="literal" >3</TT > stands for the column (or field) number. The first row and column are indexed at 0. </P ><P > If <TT CLASS="constant" ><B >DBX_RESULT_ASSOC</B ></TT > is also specified, the returning object contains the information related to <TT CLASS="constant" ><B >DBX_RESULT_INFO</B ></TT > too, even if it was not specified. </P ></DD ><DT ><TT CLASS="constant" ><B >DBX_RESULT_INFO</B ></TT ></DT ><DD ><P > It provides info about columns, such as field names and field types. </P ></DD ><DT ><TT CLASS="constant" ><B >DBX_RESULT_ASSOC</B ></TT ></DT ><DD ><P > 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. </P ><P > Associated results are actually references to the numerically indexed data, so modifying <TT CLASS="literal" >data[0][0]</TT > causes that <TT CLASS="literal" >data[0]['field_name_for_first_column']</TT > is modified as well. </P ></DD ><DT ><TT CLASS="constant" ><B >DBX_COLNAMES_UNCHANGED</B ></TT > (available from PHP 4.3.0)</DT ><DD ><P > The case of the returned column names will not be changed. </P ></DD ><DT ><TT CLASS="constant" ><B >DBX_COLNAMES_UPPERCASE</B ></TT > (available from PHP 4.3.0)</DT ><DD ><P > The case of the returned column names will be changed to uppercase. </P ></DD ><DT ><TT CLASS="constant" ><B >DBX_COLNAMES_LOWERCASE</B ></TT > (available from PHP 4.3.0)</DT ><DD ><P > The case of the returned column names will be changed to lowercase. </P ></DD ></DL ></DIV > Note that <TT CLASS="constant" ><B >DBX_RESULT_INDEX</B ></TT > is always used, regardless of the actual value of <TT CLASS="parameter" ><I >flags</I ></TT > parameter. This means that the following combinations is effective only: <P ></P ><UL ><LI ><P > <TT CLASS="constant" ><B >DBX_RESULT_INDEX</B ></TT > </P ></LI ><LI ><P > <TT CLASS="constant" ><B >DBX_RESULT_INDEX</B ></TT > | <TT CLASS="constant" ><B >DBX_RESULT_INFO</B ></TT > </P ></LI ><LI ><P > <TT CLASS="constant" ><B >DBX_RESULT_INDEX</B ></TT > | <TT CLASS="constant" ><B >DBX_RESULT_INFO</B ></TT > | <TT CLASS="constant" ><B >DBX_RESULT_ASSOC</B ></TT > - this is the default, if <TT CLASS="parameter" ><I >flags</I ></TT > is not specified. </P ></LI ></UL > </P ><P > The returing <TT CLASS="varname" >object</TT > has four or five properties depending on <TT CLASS="parameter" ><I >flags</I ></TT >: <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><SPAN CLASS="property" >handle</SPAN ></DT ><DD ><P > 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" ><A NAME="AEN18136" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$result = dbx_query ($link, "SELECT id FROM table"); mysql_field_len ($result->handle, 0);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DD ><DT ><SPAN CLASS="property" >cols</SPAN > and <SPAN CLASS="property" >rows</SPAN ></DT ><DD ><P > These contain the number of columns (or fields) and rows (or records) respectively. <DIV CLASS="informalexample" ><A NAME="AEN18144" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$result = dbx_query ($link, 'SELECT id FROM table'); echo $result->rows; // number of records echo $result->cols; // number of fields</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DD ><DT ><SPAN CLASS="property" >info</SPAN > (optional)</DT ><DD ><P > It is returned only if either <TT CLASS="constant" ><B >DBX_RESULT_INFO</B ></TT > or <TT CLASS="constant" ><B >DBX_RESULT_ASSOC</B ></TT > is specified in the <TT CLASS="parameter" ><I >flags</I ></TT > parameter. It is a 2 dimensional array, that has two named rows (<TT CLASS="literal" >name</TT > and <TT CLASS="literal" >type</TT >) to retrieve column information. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN18156" ></A ><P ><B >Example 2. lists each field's name and type</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$result = dbx_query ($link, 'SELECT id FROM table', DBX_RESULT_INDEX | DBX_RESULT_INFO); for ($i = 0; $i < $result->cols; $i++ ) { echo $result->info['name'][$i] . "\n"; echo $result->info['type'][$i] . "\n"; }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DD ><DT ><SPAN CLASS="property" >data</SPAN ></DT ><DD ><P > This property contains the actual resulting data, possibly associated with column names as well depending on <TT CLASS="parameter" ><I >flags</I ></TT >. If <TT CLASS="constant" ><B >DBX_RESULT_ASSOC</B ></TT > is set, it is possible to use <TT CLASS="literal" >$result->data[2]["field_name"]</TT >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN18167" ></A ><P ><B >Example 3. outputs the content of data property into HTML table</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$result = dbx_query ($link, 'SELECT id, parentid, description FROM table'); echo "<table>\n"; foreach ( $result->data as $row ) { echo "<tr>\n"; foreach ( $row as $field ) { echo "<td>$field</td>"; } echo "</tr>\n"; } echo "</table>\n";</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DD ></DL ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Always refer to the module-specific documentation as well. </P ><P > Column names for queries on an Oracle database are returned in lowercase. </P ></BLOCKQUOTE ></DIV ><P > See also: <A HREF="function.dbx-escape-string.html" ><B CLASS="function" >dbx_escape_string()</B ></A > and <A HREF="function.dbx-connect.html" ><B CLASS="function" >dbx_connect()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.dbx-escape-string.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.dbx-sort.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >dbx_escape_string</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.dbx.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >dbx_sort</TD ></TR ></TABLE ></DIV ></BODY ></HTML >