Zend_Db_Statement.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.statement">
  4. <title>Zend_Db_Statement</title>
  5. <para>
  6. In addition to convenient methods such as <methodname>fetchAll()</methodname> and
  7. <methodname>insert()</methodname> documented in <xref linkend="zend.db.adapter" />,
  8. you can use a statement object to gain more options for running
  9. queries and fetching result sets. This section describes how to get an
  10. instance of a statement object, and how to use its methods.
  11. </para>
  12. <para>
  13. <classname>Zend_Db_Statement</classname> is based on the PDOStatement object in the
  14. <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink> extension.
  15. </para>
  16. <sect2 id="zend.db.statement.creating">
  17. <title>Creating a Statement</title>
  18. <para>
  19. Typically, a statement object is returned by the
  20. <methodname>query()</methodname> method of the database Adapter class.
  21. This method is a general way to prepare any <acronym>SQL</acronym> statement.
  22. The first argument is a string containing an <acronym>SQL</acronym> statement.
  23. The optional second argument is an array of values to bind
  24. to parameter placeholders in the <acronym>SQL</acronym> string.
  25. </para>
  26. <example id="zend.db.statement.creating.example1">
  27. <title>Creating a SQL statement object with query()</title>
  28. <programlisting language="php"><![CDATA[
  29. $stmt = $db->query(
  30. 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
  31. array('goofy', 'FIXED')
  32. );
  33. ]]></programlisting>
  34. </example>
  35. <para>
  36. The statement object corresponds to a <acronym>SQL</acronym> statement that has been
  37. prepared, and executed once with the bind-values specified.
  38. If the statement was a SELECT query or other type of statement
  39. that returns a result set, it is now ready to fetch results.
  40. </para>
  41. <para>
  42. You can create a statement with its constructor, but this is less
  43. typical usage. There is no factory method to create this object,
  44. so you need to load the specific statement class and call its
  45. constructor. Pass the Adapter object as the first argument, and
  46. a string containing an <acronym>SQL</acronym> statement as the second argument.
  47. The statement is prepared, but not executed.
  48. </para>
  49. <example id="zend.db.statement.creating.example2">
  50. <title>Using a SQL statement constructor</title>
  51. <programlisting language="php"><![CDATA[
  52. $sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
  53. $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
  54. ]]></programlisting>
  55. </example>
  56. </sect2>
  57. <sect2 id="zend.db.statement.executing">
  58. <title>Executing a Statement</title>
  59. <para>
  60. You need to execute a statement object if you create it using its
  61. constructor, or if you want to execute the same statement multiple
  62. times. Use the <methodname>execute()</methodname> method of the statement
  63. object. The single argument is an array of value to bind to
  64. parameter placeholders in the statement.
  65. </para>
  66. <para>
  67. If you use <emphasis>positional parameters</emphasis>, or those
  68. that are marked with a question mark symbol ('<emphasis>?</emphasis>'), pass
  69. the bind values in a plain array.
  70. </para>
  71. <example id="zend.db.statement.executing.example1">
  72. <title>Executing a statement with positional parameters</title>
  73. <programlisting language="php"><![CDATA[
  74. $sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
  75. $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
  76. $stmt->execute(array('goofy', 'FIXED'));
  77. ]]></programlisting>
  78. </example>
  79. <para>
  80. If you use <emphasis>named parameters</emphasis>, or those that are
  81. indicated by a string identifier preceded by a colon character
  82. ('<emphasis>:</emphasis>'), pass the bind values in an associative array.
  83. The keys of this array should match the parameter names.
  84. </para>
  85. <example id="zend.db.statement.executing.example2">
  86. <title>Executing a statement with named parameters</title>
  87. <programlisting language="php"><![CDATA[
  88. $sql = 'SELECT * FROM bugs WHERE ' .
  89. 'reported_by = :reporter AND bug_status = :status';
  90. $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
  91. $stmt->execute(array(':reporter' => 'goofy', ':status' => 'FIXED'));
  92. ]]></programlisting>
  93. </example>
  94. <para>
  95. <acronym>PDO</acronym> statements support both positional parameters and named
  96. parameters, but not both types in a single <acronym>SQL</acronym> statement. Some of
  97. the <classname>Zend_Db_Statement</classname> classes for non-PDO extensions may support
  98. only one type of parameter or the other.
  99. </para>
  100. </sect2>
  101. <sect2 id="zend.db.statement.fetching">
  102. <title>Fetching Results from a SELECT Statement</title>
  103. <para>
  104. You can call methods on the statement object to retrieve rows from
  105. <acronym>SQL</acronym> statements that produce result set. SELECT, SHOW, DESCRIBE and
  106. EXPLAIN are examples of statements that produce a result set.
  107. INSERT, UPDATE, and DELETE are examples of statements that don't
  108. produce a result set. You can execute the latter <acronym>SQL</acronym> statements
  109. using <classname>Zend_Db_Statement</classname>, but you cannot call methods to fetch
  110. rows of results from them.
  111. </para>
  112. <sect3 id="zend.db.statement.fetching.fetch">
  113. <title>Fetching a Single Row from a Result Set</title>
  114. <para>
  115. To retrieve one row from the result set, use the
  116. <methodname>fetch()</methodname> method of the statement object.
  117. All three arguments of this method are optional:
  118. </para>
  119. <itemizedlist>
  120. <listitem>
  121. <para>
  122. <emphasis>Fetch style</emphasis> is the
  123. first argument. This controls the structure in which
  124. the row is returned.
  125. See <xref linkend="zend.db.adapter.select.fetch-mode" />
  126. for a description of the valid values and the
  127. corresponding data formats.
  128. </para>
  129. </listitem>
  130. <listitem>
  131. <para>
  132. <emphasis>Cursor orientation</emphasis>
  133. is the second argument. The default is
  134. Zend_Db::FETCH_ORI_NEXT, which simply means that each
  135. call to <methodname>fetch()</methodname> returns the next row in
  136. the result set, in the order returned by the <acronym>RDBMS</acronym>.
  137. </para>
  138. </listitem>
  139. <listitem>
  140. <para>
  141. <emphasis>Offset</emphasis> is the third
  142. argument.
  143. If the cursor orientation is Zend_Db::FETCH_ORI_ABS,
  144. then the offset number is the ordinal number of the row
  145. to return.
  146. If the cursor orientation is Zend_Db::FETCH_ORI_REL,
  147. then the offset number is relative to the cursor
  148. position before <methodname>fetch()</methodname> was called.
  149. </para>
  150. </listitem>
  151. </itemizedlist>
  152. <para>
  153. <methodname>fetch()</methodname> returns <constant>FALSE</constant> if all rows of
  154. the result set have been fetched.
  155. </para>
  156. <example id="zend.db.statement.fetching.fetch.example">
  157. <title>Using fetch() in a loop</title>
  158. <programlisting language="php"><![CDATA[
  159. $stmt = $db->query('SELECT * FROM bugs');
  160. while ($row = $stmt->fetch()) {
  161. echo $row['bug_description'];
  162. }
  163. ]]></programlisting>
  164. </example>
  165. <para>
  166. See also <ulink
  167. url="http://www.php.net/PDOStatement-fetch">PDOStatement::fetch()</ulink>.
  168. </para>
  169. </sect3>
  170. <sect3 id="zend.db.statement.fetching.fetchall">
  171. <title>Fetching a Complete Result Set</title>
  172. <para>
  173. To retrieve all the rows of the result set in one step, use the
  174. <methodname>fetchAll()</methodname> method. This is equivalent to calling
  175. the <methodname>fetch()</methodname> method in a loop and returning all
  176. the rows in an array. The <methodname>fetchAll()</methodname> method accepts
  177. two arguments. The first is the fetch style, as described above,
  178. and the second indicates the number of the column to return,
  179. when the fetch style is Zend_Db::FETCH_COLUMN.
  180. </para>
  181. <example id="zend.db.statement.fetching.fetchall.example">
  182. <title>Using fetchAll()</title>
  183. <programlisting language="php"><![CDATA[
  184. $stmt = $db->query('SELECT * FROM bugs');
  185. $rows = $stmt->fetchAll();
  186. echo $rows[0]['bug_description'];
  187. ]]></programlisting>
  188. </example>
  189. <para>
  190. See also <ulink
  191. url="http://www.php.net/PDOStatement-fetchAll">PDOStatement::fetchAll()</ulink>.
  192. </para>
  193. </sect3>
  194. <sect3 id="zend.db.statement.fetching.fetch-mode">
  195. <title>Changing the Fetch Mode</title>
  196. <para>
  197. By default, the statement object returns rows of the result set
  198. as associative arrays, mapping column names to column values.
  199. You can specify a different format for the statement class to
  200. return rows, just as you can in the Adapter class. You can use
  201. the <methodname>setFetchMode()</methodname> method of the statement object
  202. to specify the fetch mode. Specify the fetch mode using
  203. <classname>Zend_Db</classname> class constants FETCH_ASSOC, FETCH_NUM, FETCH_BOTH,
  204. FETCH_COLUMN, and FETCH_OBJ.
  205. See <xref linkend="zend.db.adapter.select.fetch-mode" />
  206. for more information on these modes.
  207. Subsequent calls to the statement methods <methodname>fetch()</methodname>
  208. or <methodname>fetchAll()</methodname> use the fetch mode that you specify.
  209. </para>
  210. <example id="zend.db.statement.fetching.fetch-mode.example">
  211. <title>Setting the fetch mode</title>
  212. <programlisting language="php"><![CDATA[
  213. $stmt = $db->query('SELECT * FROM bugs');
  214. $stmt->setFetchMode(Zend_Db::FETCH_NUM);
  215. $rows = $stmt->fetchAll();
  216. echo $rows[0][0];
  217. ]]></programlisting>
  218. </example>
  219. <para>
  220. See also <ulink
  221. url="http://www.php.net/PDOStatement-setFetchMode">PDOStatement::setFetchMode()</ulink>.
  222. </para>
  223. </sect3>
  224. <sect3 id="zend.db.statement.fetching.fetchcolumn">
  225. <title>Fetching a Single Column from a Result Set</title>
  226. <para>
  227. To return a single column from the next row of the result set,
  228. use <methodname>fetchColumn()</methodname>. The optional argument is the
  229. integer index of the column, and it defaults to 0. This method
  230. returns a scalar value, or <constant>FALSE</constant> if all rows of
  231. the result set have been fetched.
  232. </para>
  233. <para>
  234. Note this method operates differently than the
  235. <methodname>fetchCol()</methodname> method of the Adapter class.
  236. The <methodname>fetchColumn()</methodname> method of a statement returns a
  237. single value from one row.
  238. The <methodname>fetchCol()</methodname> method of an adapter returns an
  239. array of values, taken from the first column of all rows of the
  240. result set.
  241. </para>
  242. <example id="zend.db.statement.fetching.fetchcolumn.example">
  243. <title>Using fetchColumn()</title>
  244. <programlisting language="php"><![CDATA[
  245. $stmt = $db->query('SELECT bug_id, bug_description, bug_status FROM bugs');
  246. $bug_status = $stmt->fetchColumn(2);
  247. ]]></programlisting>
  248. </example>
  249. <para>
  250. See also <ulink
  251. url="http://www.php.net/PDOStatement-fetchColumn">PDOStatement::fetchColumn()</ulink>.
  252. </para>
  253. </sect3>
  254. <sect3 id="zend.db.statement.fetching.fetchobject">
  255. <title>Fetching a Row as an Object</title>
  256. <para>
  257. To retrieve a row from the result set structured as an object,
  258. use the <methodname>fetchObject()</methodname>. This method takes two
  259. optional arguments. The first argument is a string that names
  260. the class name of the object to return; the default is
  261. 'stdClass'. The second argument is an array of values that
  262. will be passed to the constructor of that class.
  263. </para>
  264. <example id="zend.db.statement.fetching.fetchobject.example">
  265. <title>Using fetchObject()</title>
  266. <programlisting language="php"><![CDATA[
  267. $stmt = $db->query('SELECT bug_id, bug_description, bug_status FROM bugs');
  268. $obj = $stmt->fetchObject();
  269. echo $obj->bug_description;
  270. ]]></programlisting>
  271. </example>
  272. <para>
  273. See also <ulink
  274. url="http://www.php.net/PDOStatement-fetchObject">PDOStatement::fetchObject()</ulink>.
  275. </para>
  276. </sect3>
  277. </sect2>
  278. <!--
  279. @todo: binding parameters is not working yet.
  280. <sect2 id="zend.db.statement.binding-param">
  281. <title>Binding PHP Variables to Parameters</title>
  282. <para>
  283. </para>
  284. <example id="zend.db.statement.binding-param.example">
  285. <title>Binding parameters from PHP variables</title>
  286. <programlisting language="php"><![CDATA[
  287. ]]></programlisting>
  288. </example>
  289. <para>
  290. See also <ulink url="http://www.php.net/PDOStatement-bindParam">PDOStatement::bindParam()</ulink>.
  291. </para>
  292. </sect2>
  293. -->
  294. <!--
  295. @todo: binding columns is not working yet.
  296. <sect2 id="zend.db.statement.binding-column">
  297. <title>Binding PHP Variables to Query Results</title>
  298. <para>
  299. </para>
  300. <example id="zend.db.statement.binding-column.example">
  301. <title>Binding results to PHP variables</title>
  302. <programlisting language="php"><![CDATA[
  303. ]]></programlisting>
  304. </example>
  305. <para>
  306. See also <ulink url="http://www.php.net/PDOStatement-bindColumn">PDOStatement::bindColumn()</ulink>.
  307. </para>
  308. </sect2>
  309. -->
  310. </sect1>