Zend_Db_Statement.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <link
  8. linkend="zend.db.adapter">Zend_Db_Adapter</link>, you can use a statement object to gain
  9. more options for running queries and fetching result sets. This section describes how to get
  10. an 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 <acronym>SELECT</acronym> 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. <acronym>SELECT</acronym>,
  106. <acronym>SHOW</acronym>, <acronym>DESCRIBE</acronym> and <acronym>EXPLAIN</acronym> are
  107. examples of statements that produce a result set. <acronym>INSERT</acronym>,
  108. <acronym>UPDATE</acronym>, and <acronym>DELETE</acronym> are examples of statements that
  109. don't produce a result set. You can execute the latter <acronym>SQL</acronym> statements
  110. using <classname>Zend_Db_Statement</classname>, but you cannot call methods to fetch
  111. rows of results from them.
  112. </para>
  113. <sect3 id="zend.db.statement.fetching.fetch">
  114. <title>Fetching a Single Row from a Result Set</title>
  115. <para>
  116. To retrieve one row from the result set, use the
  117. <methodname>fetch()</methodname> method of the statement object.
  118. All three arguments of this method are optional:
  119. </para>
  120. <itemizedlist>
  121. <listitem>
  122. <para>
  123. <emphasis>Fetch style</emphasis> is the
  124. first argument. This controls the structure in which
  125. the row is returned.
  126. See <link linkend="zend.db.adapter.select.fetch-mode">this chapter</link>
  127. for a description of the valid values and the
  128. corresponding data formats.
  129. </para>
  130. </listitem>
  131. <listitem>
  132. <para>
  133. <emphasis>Cursor orientation</emphasis>
  134. is the second argument. The default is
  135. <constant>Zend_Db::FETCH_ORI_NEXT</constant>, which simply means that each
  136. call to <methodname>fetch()</methodname> returns the next row in
  137. the result set, in the order returned by the <acronym>RDBMS</acronym>.
  138. </para>
  139. </listitem>
  140. <listitem>
  141. <para>
  142. <emphasis>Offset</emphasis> is the third
  143. argument.
  144. If the cursor orientation is <constant>Zend_Db::FETCH_ORI_ABS</constant>,
  145. then the offset number is the ordinal number of the row to return.
  146. If the cursor orientation is <constant>Zend_Db::FETCH_ORI_REL</constant>,
  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 <constant>Zend_Db::FETCH_COLUMN</constant>.
  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 <constant>FETCH_ASSOC</constant>,
  204. <constant>FETCH_NUM</constant>, <constant>FETCH_BOTH</constant>,
  205. <constant>FETCH_COLUMN</constant>, and <constant>FETCH_OBJ</constant>.
  206. See <link linkend="zend.db.adapter.select.fetch-mode">this chapter</link>
  207. for more information on these modes. Subsequent calls to the statement methods
  208. <methodname>fetch()</methodname> or <methodname>fetchAll()</methodname> use the
  209. fetch mode that you specify.
  210. </para>
  211. <example id="zend.db.statement.fetching.fetch-mode.example">
  212. <title>Setting the fetch mode</title>
  213. <programlisting language="php"><![CDATA[
  214. $stmt = $db->query('SELECT * FROM bugs');
  215. $stmt->setFetchMode(Zend_Db::FETCH_NUM);
  216. $rows = $stmt->fetchAll();
  217. echo $rows[0][0];
  218. ]]></programlisting>
  219. </example>
  220. <para>
  221. See also <ulink
  222. url="http://www.php.net/PDOStatement-setFetchMode">PDOStatement::setFetchMode()</ulink>.
  223. </para>
  224. </sect3>
  225. <sect3 id="zend.db.statement.fetching.fetchcolumn">
  226. <title>Fetching a Single Column from a Result Set</title>
  227. <para>
  228. To return a single column from the next row of the result set,
  229. use <methodname>fetchColumn()</methodname>. The optional argument is the
  230. integer index of the column, and it defaults to 0. This method
  231. returns a scalar value, or <constant>FALSE</constant> if all rows of
  232. the result set have been fetched.
  233. </para>
  234. <para>
  235. Note this method operates differently than the
  236. <methodname>fetchCol()</methodname> method of the Adapter class.
  237. The <methodname>fetchColumn()</methodname> method of a statement returns a
  238. single value from one row.
  239. The <methodname>fetchCol()</methodname> method of an adapter returns an
  240. array of values, taken from the first column of all rows of the
  241. result set.
  242. </para>
  243. <example id="zend.db.statement.fetching.fetchcolumn.example">
  244. <title>Using fetchColumn()</title>
  245. <programlisting language="php"><![CDATA[
  246. $stmt = $db->query('SELECT bug_id, bug_description, bug_status FROM bugs');
  247. $bug_status = $stmt->fetchColumn(2);
  248. ]]></programlisting>
  249. </example>
  250. <para>
  251. See also <ulink
  252. url="http://www.php.net/PDOStatement-fetchColumn">PDOStatement::fetchColumn()</ulink>.
  253. </para>
  254. </sect3>
  255. <sect3 id="zend.db.statement.fetching.fetchobject">
  256. <title>Fetching a Row as an Object</title>
  257. <para>
  258. To retrieve a row from the result set structured as an object,
  259. use the <methodname>fetchObject()</methodname>. This method takes two
  260. optional arguments. The first argument is a string that names
  261. the class name of the object to return; the default is
  262. 'stdClass'. The second argument is an array of values that
  263. will be passed to the constructor of that class.
  264. </para>
  265. <example id="zend.db.statement.fetching.fetchobject.example">
  266. <title>Using fetchObject()</title>
  267. <programlisting language="php"><![CDATA[
  268. $stmt = $db->query('SELECT bug_id, bug_description, bug_status FROM bugs');
  269. $obj = $stmt->fetchObject();
  270. echo $obj->bug_description;
  271. ]]></programlisting>
  272. </example>
  273. <para>
  274. See also <ulink
  275. url="http://www.php.net/PDOStatement-fetchObject">PDOStatement::fetchObject()</ulink>.
  276. </para>
  277. </sect3>
  278. </sect2>
  279. <!--
  280. @todo: binding parameters is not working yet.
  281. <sect2 id="zend.db.statement.binding-param">
  282. <title>Binding PHP Variables to Parameters</title>
  283. <para>
  284. </para>
  285. <example id="zend.db.statement.binding-param.example">
  286. <title>Binding parameters from PHP variables</title>
  287. <programlisting language="php"><![CDATA[
  288. ]]></programlisting>
  289. </example>
  290. <para>
  291. See also <ulink
  292. url="http://www.php.net/PDOStatement-bindParam">PDOStatement::bindParam()</ulink>.
  293. </para>
  294. </sect2>
  295. -->
  296. <!--
  297. @todo: binding columns is not working yet.
  298. <sect2 id="zend.db.statement.binding-column">
  299. <title>Binding PHP Variables to Query Results</title>
  300. <para>
  301. </para>
  302. <example id="zend.db.statement.binding-column.example">
  303. <title>Binding results to PHP variables</title>
  304. <programlisting language="php"><![CDATA[
  305. ]]></programlisting>
  306. </example>
  307. <para>
  308. See also <ulink
  309. url="http://www.php.net/PDOStatement-bindColumn">PDOStatement::bindColumn()</ulink>.
  310. </para>
  311. </sect2>
  312. -->
  313. </sect1>