| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.db.statement">
- <title>Zend_Db_Statement</title>
- <para>
- In addition to convenient methods such as <methodname>fetchAll()</methodname> and
- <methodname>insert()</methodname> documented in <link
- linkend="zend.db.adapter">Zend_Db_Adapter</link>, you can use a statement object to gain
- more options for running queries and fetching result sets. This section describes how to get
- an instance of a statement object, and how to use its methods.
- </para>
- <para>
- <classname>Zend_Db_Statement</classname> is based on the PDOStatement object in the
- <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink> extension.
- </para>
- <sect2 id="zend.db.statement.creating">
- <title>Creating a Statement</title>
- <para>
- Typically, a statement object is returned by the
- <methodname>query()</methodname> method of the database Adapter class.
- This method is a general way to prepare any <acronym>SQL</acronym> statement.
- The first argument is a string containing an <acronym>SQL</acronym> statement.
- The optional second argument is an array of values to bind
- to parameter placeholders in the <acronym>SQL</acronym> string.
- </para>
- <example id="zend.db.statement.creating.example1">
- <title>Creating a SQL statement object with query()</title>
- <programlisting language="php"><![CDATA[
- $stmt = $db->query(
- 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
- array('goofy', 'FIXED')
- );
- ]]></programlisting>
- </example>
- <para>
- The statement object corresponds to a <acronym>SQL</acronym> statement that has been
- prepared, and executed once with the bind-values specified.
- If the statement was a <acronym>SELECT</acronym> query or other type of statement
- that returns a result set, it is now ready to fetch results.
- </para>
- <para>
- You can create a statement with its constructor, but this is less
- typical usage. There is no factory method to create this object,
- so you need to load the specific statement class and call its
- constructor. Pass the Adapter object as the first argument, and
- a string containing an <acronym>SQL</acronym> statement as the second argument.
- The statement is prepared, but not executed.
- </para>
- <example id="zend.db.statement.creating.example2">
- <title>Using a SQL statement constructor</title>
- <programlisting language="php"><![CDATA[
- $sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
- $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.db.statement.executing">
- <title>Executing a Statement</title>
- <para>
- You need to execute a statement object if you create it using its
- constructor, or if you want to execute the same statement multiple
- times. Use the <methodname>execute()</methodname> method of the statement
- object. The single argument is an array of value to bind to
- parameter placeholders in the statement.
- </para>
- <para>
- If you use <emphasis>positional parameters</emphasis>, or those
- that are marked with a question mark symbol ('<emphasis>?</emphasis>'), pass
- the bind values in a plain array.
- </para>
- <example id="zend.db.statement.executing.example1">
- <title>Executing a statement with positional parameters</title>
- <programlisting language="php"><![CDATA[
- $sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
- $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
- $stmt->execute(array('goofy', 'FIXED'));
- ]]></programlisting>
- </example>
- <para>
- If you use <emphasis>named parameters</emphasis>, or those that are
- indicated by a string identifier preceded by a colon character
- ('<emphasis>:</emphasis>'), pass the bind values in an associative array.
- The keys of this array should match the parameter names.
- </para>
- <example id="zend.db.statement.executing.example2">
- <title>Executing a statement with named parameters</title>
- <programlisting language="php"><![CDATA[
- $sql = 'SELECT * FROM bugs WHERE ' .
- 'reported_by = :reporter AND bug_status = :status';
- $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
- $stmt->execute(array(':reporter' => 'goofy', ':status' => 'FIXED'));
- ]]></programlisting>
- </example>
- <para>
- <acronym>PDO</acronym> statements support both positional parameters and named
- parameters, but not both types in a single <acronym>SQL</acronym> statement. Some of
- the <classname>Zend_Db_Statement</classname> classes for non-PDO extensions may support
- only one type of parameter or the other.
- </para>
- </sect2>
- <sect2 id="zend.db.statement.fetching">
- <title>Fetching Results from a SELECT Statement</title>
- <para>
- You can call methods on the statement object to retrieve rows from
- <acronym>SQL</acronym> statements that produce result set. <acronym>SELECT</acronym>,
- <acronym>SHOW</acronym>, <acronym>DESCRIBE</acronym> and <acronym>EXPLAIN</acronym> are
- examples of statements that produce a result set. <acronym>INSERT</acronym>,
- <acronym>UPDATE</acronym>, and <acronym>DELETE</acronym> are examples of statements that
- don't produce a result set. You can execute the latter <acronym>SQL</acronym> statements
- using <classname>Zend_Db_Statement</classname>, but you cannot call methods to fetch
- rows of results from them.
- </para>
- <sect3 id="zend.db.statement.fetching.fetch">
- <title>Fetching a Single Row from a Result Set</title>
- <para>
- To retrieve one row from the result set, use the
- <methodname>fetch()</methodname> method of the statement object.
- All three arguments of this method are optional:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <emphasis>Fetch style</emphasis> is the
- first argument. This controls the structure in which
- the row is returned.
- See <link linkend="zend.db.adapter.select.fetch-mode">this chapter</link>
- for a description of the valid values and the
- corresponding data formats.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>Cursor orientation</emphasis>
- is the second argument. The default is
- <constant>Zend_Db::FETCH_ORI_NEXT</constant>, which simply means that each
- call to <methodname>fetch()</methodname> returns the next row in
- the result set, in the order returned by the <acronym>RDBMS</acronym>.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>Offset</emphasis> is the third
- argument.
- If the cursor orientation is <constant>Zend_Db::FETCH_ORI_ABS</constant>,
- then the offset number is the ordinal number of the row to return.
- If the cursor orientation is <constant>Zend_Db::FETCH_ORI_REL</constant>,
- then the offset number is relative to the cursor
- position before <methodname>fetch()</methodname> was called.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- <methodname>fetch()</methodname> returns <constant>FALSE</constant> if all rows of
- the result set have been fetched.
- </para>
- <example id="zend.db.statement.fetching.fetch.example">
- <title>Using fetch() in a loop</title>
- <programlisting language="php"><![CDATA[
- $stmt = $db->query('SELECT * FROM bugs');
- while ($row = $stmt->fetch()) {
- echo $row['bug_description'];
- }
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-fetch">PDOStatement::fetch()</ulink>.
- </para>
- </sect3>
- <sect3 id="zend.db.statement.fetching.fetchall">
- <title>Fetching a Complete Result Set</title>
- <para>
- To retrieve all the rows of the result set in one step, use the
- <methodname>fetchAll()</methodname> method. This is equivalent to calling
- the <methodname>fetch()</methodname> method in a loop and returning all
- the rows in an array. The <methodname>fetchAll()</methodname> method accepts
- two arguments. The first is the fetch style, as described above,
- and the second indicates the number of the column to return,
- when the fetch style is <constant>Zend_Db::FETCH_COLUMN</constant>.
- </para>
- <example id="zend.db.statement.fetching.fetchall.example">
- <title>Using fetchAll()</title>
- <programlisting language="php"><![CDATA[
- $stmt = $db->query('SELECT * FROM bugs');
- $rows = $stmt->fetchAll();
- echo $rows[0]['bug_description'];
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-fetchAll">PDOStatement::fetchAll()</ulink>.
- </para>
- </sect3>
- <sect3 id="zend.db.statement.fetching.fetch-mode">
- <title>Changing the Fetch Mode</title>
- <para>
- By default, the statement object returns rows of the result set
- as associative arrays, mapping column names to column values.
- You can specify a different format for the statement class to
- return rows, just as you can in the Adapter class. You can use
- the <methodname>setFetchMode()</methodname> method of the statement object
- to specify the fetch mode. Specify the fetch mode using
- <classname>Zend_Db</classname> class constants <constant>FETCH_ASSOC</constant>,
- <constant>FETCH_NUM</constant>, <constant>FETCH_BOTH</constant>,
- <constant>FETCH_COLUMN</constant>, and <constant>FETCH_OBJ</constant>.
- See <link linkend="zend.db.adapter.select.fetch-mode">this chapter</link>
- for more information on these modes. Subsequent calls to the statement methods
- <methodname>fetch()</methodname> or <methodname>fetchAll()</methodname> use the
- fetch mode that you specify.
- </para>
- <example id="zend.db.statement.fetching.fetch-mode.example">
- <title>Setting the fetch mode</title>
- <programlisting language="php"><![CDATA[
- $stmt = $db->query('SELECT * FROM bugs');
- $stmt->setFetchMode(Zend_Db::FETCH_NUM);
- $rows = $stmt->fetchAll();
- echo $rows[0][0];
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-setFetchMode">PDOStatement::setFetchMode()</ulink>.
- </para>
- </sect3>
- <sect3 id="zend.db.statement.fetching.fetchcolumn">
- <title>Fetching a Single Column from a Result Set</title>
- <para>
- To return a single column from the next row of the result set,
- use <methodname>fetchColumn()</methodname>. The optional argument is the
- integer index of the column, and it defaults to 0. This method
- returns a scalar value, or <constant>FALSE</constant> if all rows of
- the result set have been fetched.
- </para>
- <para>
- Note this method operates differently than the
- <methodname>fetchCol()</methodname> method of the Adapter class.
- The <methodname>fetchColumn()</methodname> method of a statement returns a
- single value from one row.
- The <methodname>fetchCol()</methodname> method of an adapter returns an
- array of values, taken from the first column of all rows of the
- result set.
- </para>
- <example id="zend.db.statement.fetching.fetchcolumn.example">
- <title>Using fetchColumn()</title>
- <programlisting language="php"><![CDATA[
- $stmt = $db->query('SELECT bug_id, bug_description, bug_status FROM bugs');
- $bug_status = $stmt->fetchColumn(2);
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-fetchColumn">PDOStatement::fetchColumn()</ulink>.
- </para>
- </sect3>
- <sect3 id="zend.db.statement.fetching.fetchobject">
- <title>Fetching a Row as an Object</title>
- <para>
- To retrieve a row from the result set structured as an object,
- use the <methodname>fetchObject()</methodname>. This method takes two
- optional arguments. The first argument is a string that names
- the class name of the object to return; the default is
- 'stdClass'. The second argument is an array of values that
- will be passed to the constructor of that class.
- </para>
- <example id="zend.db.statement.fetching.fetchobject.example">
- <title>Using fetchObject()</title>
- <programlisting language="php"><![CDATA[
- $stmt = $db->query('SELECT bug_id, bug_description, bug_status FROM bugs');
- $obj = $stmt->fetchObject();
- echo $obj->bug_description;
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-fetchObject">PDOStatement::fetchObject()</ulink>.
- </para>
- </sect3>
- </sect2>
- <!--
- @todo: binding parameters is not working yet.
- <sect2 id="zend.db.statement.binding-param">
- <title>Binding PHP Variables to Parameters</title>
- <para>
- </para>
- <example id="zend.db.statement.binding-param.example">
- <title>Binding parameters from PHP variables</title>
- <programlisting language="php"><![CDATA[
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-bindParam">PDOStatement::bindParam()</ulink>.
- </para>
- </sect2>
- -->
- <!--
- @todo: binding columns is not working yet.
- <sect2 id="zend.db.statement.binding-column">
- <title>Binding PHP Variables to Query Results</title>
- <para>
- </para>
- <example id="zend.db.statement.binding-column.example">
- <title>Binding results to PHP variables</title>
- <programlisting language="php"><![CDATA[
- ]]></programlisting>
- </example>
- <para>
- See also <ulink
- url="http://www.php.net/PDOStatement-bindColumn">PDOStatement::bindColumn()</ulink>.
- </para>
- </sect2>
- -->
- </sect1>
|