<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >sesam_query</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="SESAM Database Functions" HREF="ref.sesam.html"><LINK REL="PREVIOUS" TITLE="sesam_num_fields" HREF="function.sesam-num-fields.html"><LINK REL="NEXT" TITLE="sesam_rollback" HREF="function.sesam-rollback.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.sesam-num-fields.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.sesam-rollback.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.sesam-query" ></A >sesam_query</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN207138" ></A ><P > (PHP 3 CVS only)</P >sesam_query -- Perform a SESAM SQL query and prepare the result</DIV ><DIV CLASS="refsect1" ><A NAME="AEN207141" ></A ><H2 >说明</H2 >string <B CLASS="methodname" >sesam_query</B > ( string query [, bool scrollable] )<BR ></BR ><P > <B CLASS="function" >sesam_query()</B > sends a query to the currently active database on the server. It can execute both "immediate" SQL statements and "select type" queries. If an "immediate" statement is executed, then no cursor is allocated, and any subsequent <A HREF="function.sesam-fetch-row.html" ><B CLASS="function" >sesam_fetch_row()</B ></A > or <A HREF="function.sesam-fetch-result.html" ><B CLASS="function" >sesam_fetch_result()</B ></A > call will return an empty result (zero columns, indicating end-of-result). </P ><P > When using "scrollable" cursors, the cursor can be freely positioned on the result set. For each "scrollable" query, there are global default values for the scrolling type (initialized to: <TT CLASS="literal" >SESAM_SEEK_NEXT</TT >) and the scrolling offset which can either be set once by <A HREF="function.sesam-seek-row.html" ><B CLASS="function" >sesam_seek_row()</B ></A > or each time when fetching a row using <A HREF="function.sesam-fetch-row.html" ><B CLASS="function" >sesam_fetch_row()</B ></A >. </P ><P > For "immediate" statements, the number of affected rows is saved for retrieval by the <A HREF="function.sesam-affected-rows.html" ><B CLASS="function" >sesam_affected_rows()</B ></A > function. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN207162" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >query</CODE ></DT ><DD ><P > The query statement. </P ></DD ><DT ><CODE CLASS="parameter" >scrollable</CODE ></DT ><DD ><P > For "select type" statements, a result descriptor and a (scrollable or sequential, depending on the optional boolean <CODE CLASS="parameter" >scrollable</CODE > parameter) cursor will be allocated. If <CODE CLASS="parameter" >scrollable</CODE > is omitted, the cursor will be sequential. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN207178" ></A ><H2 >返回值</H2 ><P > Returns a SESAM "result identifier" on success, or <TT CLASS="constant" ><B >FALSE</B ></TT > on error. This result id is used by the other functions. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN207182" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN207185" ></A ><P ><B >例 1. Show all rows of the "phone" table as a HTML table </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">if (!</font><font color="#0000BB">sesam_connect</font><font color="#007700">(</font><font color="#DD0000">"phonedb"</font><font color="#007700">, </font><font color="#DD0000">"demo"</font><font color="#007700">, </font><font color="#DD0000">"otto"</font><font color="#007700">))<br /> die(</font><font color="#DD0000">"cannot connect"</font><font color="#007700">);<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">sesam_query</font><font color="#007700">(</font><font color="#DD0000">"select * from phone"</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$result</font><font color="#007700">) {<br /> </font><font color="#0000BB">$err </font><font color="#007700">= </font><font color="#0000BB">sesam_diagnostic</font><font color="#007700">();<br /> die (</font><font color="#0000BB">$err</font><font color="#007700">[</font><font color="#DD0000">"errmsg"</font><font color="#007700">]);<br />}<br />echo </font><font color="#DD0000">"<table border>\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// Add title header with column names above the result:<br /></font><font color="#007700">if (</font><font color="#0000BB">$cols </font><font color="#007700">= </font><font color="#0000BB">sesam_field_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"<tr><th colspan=" </font><font color="#007700">. </font><font color="#0000BB">$cols</font><font color="#007700">[</font><font color="#DD0000">"count"</font><font color="#007700">] . </font><font color="#DD0000">">Result:</th></tr>\n"</font><font color="#007700">;<br /> echo </font><font color="#DD0000">"<tr>\n"</font><font color="#007700">;<br /> for (</font><font color="#0000BB">$col </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$col </font><font color="#007700">< </font><font color="#0000BB">$cols</font><font color="#007700">[</font><font color="#DD0000">"count"</font><font color="#007700">]; ++</font><font color="#0000BB">$col</font><font color="#007700">) {<br /> </font><font color="#0000BB">$colattr </font><font color="#007700">= </font><font color="#0000BB">$cols</font><font color="#007700">[</font><font color="#0000BB">$col</font><font color="#007700">];<br /> </font><font color="#FF8000">/* Span the table head over SESAM's "Multiple Fields": */<br /> </font><font color="#007700">if (</font><font color="#0000BB">$colattr</font><font color="#007700">[</font><font color="#DD0000">"count"</font><font color="#007700">] > </font><font color="#0000BB">1</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"<th colspan=\"" </font><font color="#007700">. </font><font color="#0000BB">$colattr</font><font color="#007700">[</font><font color="#DD0000">"count"</font><font color="#007700">] . </font><font color="#DD0000">"\">" </font><font color="#007700">. </font><font color="#0000BB">$colattr</font><font color="#007700">[</font><font color="#DD0000">"name"</font><font color="#007700">] .<br /> </font><font color="#DD0000">"(1.." </font><font color="#007700">. </font><font color="#0000BB">$colattr</font><font color="#007700">[</font><font color="#DD0000">"count"</font><font color="#007700">] . </font><font color="#DD0000">")</th>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$col </font><font color="#007700">+= </font><font color="#0000BB">$colattr</font><font color="#007700">[</font><font color="#DD0000">"count"</font><font color="#007700">] - </font><font color="#0000BB">1</font><font color="#007700">;<br /> } else<br /> echo </font><font color="#DD0000">"<th>" </font><font color="#007700">. </font><font color="#0000BB">$colattr</font><font color="#007700">[</font><font color="#DD0000">"name"</font><font color="#007700">] . </font><font color="#DD0000">"</th>\n"</font><font color="#007700">;<br /> }<br /> echo </font><font color="#DD0000">"</tr>\n"</font><font color="#007700">;<br />}<br /><br />do {<br /> </font><font color="#FF8000">// Fetch the result in chunks of 100 rows max.<br /> </font><font color="#0000BB">$ok </font><font color="#007700">= </font><font color="#0000BB">sesam_fetch_result</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">100</font><font color="#007700">);<br /> for (</font><font color="#0000BB">$row</font><font color="#007700">=</font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$row </font><font color="#007700">< </font><font color="#0000BB">$ok</font><font color="#007700">[</font><font color="#DD0000">"rows"</font><font color="#007700">]; ++</font><font color="#0000BB">$row</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">" <tr>\n"</font><font color="#007700">;<br /> for (</font><font color="#0000BB">$col </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$col </font><font color="#007700">< </font><font color="#0000BB">$ok</font><font color="#007700">[</font><font color="#DD0000">"cols"</font><font color="#007700">]; ++</font><font color="#0000BB">$col</font><font color="#007700">) {<br /> if (isset(</font><font color="#0000BB">$ok</font><font color="#007700">[</font><font color="#0000BB">$col</font><font color="#007700">][</font><font color="#0000BB">$row</font><font color="#007700">])) {<br /> echo </font><font color="#DD0000">"<td>" </font><font color="#007700">. </font><font color="#0000BB">$ok</font><font color="#007700">[</font><font color="#0000BB">$col</font><font color="#007700">][</font><font color="#0000BB">$row</font><font color="#007700">] . </font><font color="#DD0000">"</td>\n"</font><font color="#007700">;<br /> } else {<br /> echo </font><font color="#DD0000">"<td>-empty-</td>\n"</font><font color="#007700">;<br /> }<br /> }<br /> echo </font><font color="#DD0000">"</tr>\n"</font><font color="#007700">;<br /> }<br />} while (</font><font color="#0000BB">$ok</font><font color="#007700">[</font><font color="#DD0000">"truncated"</font><font color="#007700">]); </font><font color="#FF8000">// while there may be more data<br /><br /></font><font color="#007700">echo </font><font color="#DD0000">"</table>\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// free result id<br /></font><font color="#0000BB">sesam_free_result</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN207188" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.sesam-fetch-row.html" ><B CLASS="function" >sesam_fetch_row()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.sesam-fetch-result.html" ><B CLASS="function" >sesam_fetch_result()</B ></A ></TD ></TR ></TBODY ></TABLE ><P ></P > </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.sesam-num-fields.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.sesam-rollback.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >sesam_num_fields</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.sesam.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >sesam_rollback</TD ></TR ></TABLE ></DIV ></BODY ></HTML >