cvs: peardoc /en/package/database/db intro-execute.xml

From: Date: Tue, 13 Apr 2004 21:01:28 +0000
Subject: cvs: peardoc /en/package/database/db intro-execute.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-3378@lists.php.net to get a copy of this message
danielc Tue Apr 13 17:01:28 2004 EDT Modified files: /peardoc/en/package/database/db intro-execute.xml Log: Mention ability to pass scalars to execute. Clarify execute/executemultiple text and examples. http://cvs.php.net/diff.php/peardoc/en/package/database/db/intro-execute.xml?r1=1.19&r2=1.20&ty=u Index: peardoc/en/package/database/db/intro-execute.xml diff -u peardoc/en/package/database/db/intro-execute.xml:1.19 peardoc/en/package/database/db/intro-execute.xml:1.20 --- peardoc/en/package/database/db/intro-execute.xml:1.19 Tue Apr 13 14:55:00 2004 +++ peardoc/en/package/database/db/intro-execute.xml Tue Apr 13 17:01:27 2004 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.19 $ --> +<!-- $Revision: 1.20 $ --> <refentry id="package.database.db.intro-execute"> <refnamediv> @@ -112,21 +112,67 @@ </refsect2> <refsect2> - <title>Execute/ ExecuteMultiple</title> + <title>Execute</title> <para> After preparing the statement, you can execute the query. This means to assign the variables to the prepared statement. To do this, <link linkend="package.database.db.db-common.execute"> <function>execute</function></link> requires two arguments, the statement handle of <link linkend="package.database.db.db-common.prepare"> - <function>prepare</function></link> and an array - with the values to assign. The array has to be numerically - ordered. The first entry of the array represents the first - wildcard, the second the second wildcard etc. The order is - independent from the used wildcard char. + <function>prepare</function></link> and a scalar or array + with the values to assign. + </para> + + <para> + <example> + <title>Passing scalars to <function>execute</function></title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$sth = $db->prepare('INSERT INTO numbers (number) VALUES (?)'); +$db->execute($sth, 1); +$db->execute($sth, 8); +?> + ]]> + </programlisting> + </example> + </para> + + <para> + When a prepared statement has multiple placeholders, you must use + an array to pass the values to <function>execute</function>. + The first entry of the array represents the first + placeholder, the second the second placeholder, etc. + The order is independent of the type of placeholder used. + </para> + + <para> + <example> + <title>Passing an array to <function>execute</function></title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid DB object named $db... +$sth = $db->prepare('INSERT INTO numbers VALUES (?, ?, ?)'); + +$data = array(1, 'one', 'en'); +$db->execute($sth, $row); +?> + ]]> + </programlisting> + </example> + </para> + </refsect2> + + <refsect2> + <title>ExecuteMultiple</title> + <para> + DB contains a process for executing several queries at once. + So, rather than having to execute them manually, like this: <example> - <title>Inserting data into a database</title> + <title>Passing arrays to <function>execute</function></title> <programlisting role="php"> <![CDATA[ // Once you have a valid DB object named $db... @@ -142,7 +188,7 @@ </programlisting> </example> - In the example the query is done four times: + which would issue four queries: <programlisting role="sql"> <![CDATA[ INSERT INTO numbers VALUES ('1', 'one', 'en') @@ -151,13 +197,11 @@ INSERT INTO numbers VALUES ('4', 'four', 'fire') ]]> </programlisting> - </para> - <para> + you can use <link linkend="package.database.db.db-common.executemultiple"> - <function>executeMultiple</function></link> works in the same way, but - requires a two dimensional array. So you can avoid the explicit - foreach in the eample above. + <function>executeMultiple</function></link> to avoid the explicit + foreach in the eample above: <example> <title>

« previous php.pear.doc (#3378) next »