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

From: Date: Fri, 05 May 2006 14:44:53 +0000
Subject: cvs: peardoc /en/package/database mdb2.xml /en/package/database/mdb2 intro-execute.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-7880@lists.php.net to get a copy of this message
lsmith Fri May 5 14:44:53 2006 UTC Modified files: /peardoc/en/package/database/mdb2 intro-execute.xml /peardoc/en/package/database mdb2.xml Log: - ported execute tutorial to MDB2 ..

http://cvs.php.net/viewcvs.cgi/peardoc/en/package/database/mdb2/intro-execute.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/database/mdb2/intro-execute.xml diff -u peardoc/en/package/database/mdb2/intro-execute.xml:1.2 peardoc/en/package/database/mdb2/intro-execute.xml:1.3 --- peardoc/en/package/database/mdb2/intro-execute.xml:1.2 Fri May 5 11:57:32 2006 +++ peardoc/en/package/database/mdb2/intro-execute.xml Fri May 5 14:44:53 2006 @@ -1,7 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1" ?> -<!-- $Revision: 1.2 $ --> - -<!-- needs fixing for MDB2--> +<!-- $Revision: 1.3 $ --> <refentry id="package.database.mdb2.intro-execute"> <refnamediv> @@ -46,6 +44,17 @@ SET surname=?, name=?, age=?'; ]]> </programlisting> + Furthermore it is also possible to use named placeholders as inspired + by Oracle: + <programlisting role="php"> + <![CDATA[ +$statement['db1']['INSERT_PERSON'] = 'INSERT INTO person + (surname, name, age) VALUES (:surname, :lastname, :age)'; + +$statement['db2']['INSERT_PERSON'] = 'INSERT INTO person + SET surname=:surname, name=:lastname, age=:age'; + ]]> + </programlisting> </para> </refsect2> @@ -81,39 +90,31 @@ Then pass this SQL statement to <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodprepare"> <function>prepare</function></ulink>, which returns - a statement handle to be used when calling + a statement class instance to be used when calling <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodexecute"> <function>execute</function></ulink>. </para> <para> <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodprepare"> <function>prepare</function></ulink> can handle different types of - placeholders (a.k.a. wildcards). - <simplelist> - <member> - <literal>?</literal> - (recommended) stands for a scalar - value like strings or numbers. The value will be automatically - escaped and quoted according to the current DBMS's requirements. - </member> - <member> - <literal>!</literal> - stands for a scalar value and - will inserted into the statement <quote>as is</quote>. - </member> - <member> - <literal>&amp;</literal> - requires an existing filename, - the content of this file will be included into the statment - (i.e. for saving binary data of a graphic file in a database) - </member> - </simplelist> + placeholders (a.k.a. wildcards). By default all placeholders are handled + as strings. However passing an array of + <ulink url="http://cvs.php.net/viewcvs.cgi/*checkout*/pear/MDB2/docs/datatypes.html"> + <function>data types</function></ulink> as the second parameter makes it + possible to set a specific type for each placeholder. </para> <para> - Use backslashes to escape placeholder characters if you don't want - them to be interpreted as placeholders: - <programlisting role="sql"> - <![CDATA[ -UPDATE foo SET col=? WHERE col='over \& under' - ]]> - </programlisting> + Since DML (data manipulation language - INSERT, UPDATE, DELETE) statements + have different return values than data fetches the + <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodprepare"> + <function>prepare</function></ulink> accepts a third parameter. This + parameter should be set to <constant>MDB2_PREPARE_MANIP</constant> for DML + statements. For data reads it should either be set to + <constant>MDB2_PREPARE_RESULT</constant>, an array of + <ulink url="http://cvs.php.net/viewcvs.cgi/*checkout*/pear/MDB2/docs/datatypes.html"> + <function>data types</function></ulink> for each of the columns in the + result set or &true; in order to automatically detect the data types in the + result set. </para> </refsect2> @@ -123,11 +124,8 @@ After preparing the statement, you can execute the query. This means to assign the variables to the prepared statement. To do this, <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodexecute"> - <function>execute</function></ulink> requires two arguments: - the statement handle returned by - <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Common.html#methodprepare"> - <function>prepare</function></ulink> and a scalar or array - with the values to assign. + <function>execute</function></ulink> requires one argument a scalar or + array with the values to assign. </para> <para> @@ -137,9 +135,9 @@ <![CDATA[ <?php // Once you have a valid MDB2 object named $mdb2... -$sth = $mdb2->prepare('INSERT INTO numbers (number) VALUES (?)'); -$mdb2->execute($sth, 1); -$mdb2->execute($sth, 8); +$sth = $mdb2->prepare('INSERT INTO numbers (number) VALUES (?)', array('integer'), MDB2_PREPARE_MANIP); +$sth->execute(1); +$sth->execute(8); ?> ]]> </programlisting> @@ -161,14 +159,58 @@ <![CDATA[ <?php // Once you have a valid MDB2 object named $mdb2... -$sth = $mdb2->prepare('INSERT INTO numbers VALUES (?, ?, ?)'); +$types = array('integer', 'text', 'text'); +$sth = $mdb2->prepare('INSERT INTO numbers VALUES (?, ?, ?)', $types, MDB2_PREPARE_MANIP); $data = array(1, 'one', 'en'); -$mdb2->execute($sth, $data); +$affectedRows = $sth->execute($data); +?> + ]]> + </programlisting> + </example> + + <para> + When using named placeholders the data array needs to be an associative + array with the keys matching the placeholder names. + </para> + + <para> + <example> + <title>Passing an array to <function>execute</function></title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid MDB2 object named $mdb2... +$types = array('integer', 'text', 'text'); +$sth = $mdb2->prepare('INSERT INTO numbers VALUES (:id, :text, :lang)'); + +$data = array('id' => 1, 'name' => 'one', 'lang' => 'en'); +$affectedRows = $sth->execute($data); +?> + ]]> + </programlisting> + </example> + + <para> + When using named placeholders the data array needs to be an associative + array with the keys matching the placeholder names. + </para> + + <para> + <example> + <title>Passing an array to <function>execute</function></title> + <programlisting role="php"> + <![CDATA[ +<?php +// Once you have a valid MDB2 object named $mdb2... +$sth = $mdb2->prepare('SELECT name, lang FROM numbers WHERE id = ?', array('integer'), array('text', 'text')); + +$result = $sth->execute(1); ?> ]]> </programlisting> </example> + </para> <warning> @@ -224,18 +266,19 @@ <example> <title> - Using <function>executeMultiple</function> instead of - <function>execute</function> + Using <function>executeMultiple</function> from the Extended Module + instead of <function>execute</function> </title> <programlisting role="php"> <![CDATA[ // Once you have a valid MDB2 object named $mdb2... +$mdb->loadModule('Exended', null, false); $alldata = array(array(1, 'one', 'en'), array(2, 'two', 'to'), array(3, 'three', 'tre'), array(4, 'four', 'fire')); $sth = $mdb2->prepare('INSERT INTO numbers VALUES (?, ?, ?)'); -$mdb2->executeMultiple($sth, $alldata); +$mdb2->extended->executeMultiple($sth, $alldata); ]]> </programlisting> </example> @@ -253,9 +296,8 @@ a new MDB2_Result_Common object for queries that return results (such as <command>SELECT</command> queries), - &constant.db-ok; for queries that manipulate data (such as - <command>INSERT</command> queries) - or a MDB2_Error object on failure + integer for queries that manipulate data (such as + <command>INSERT</command> queries) or a MDB2_Error object on failure </para> </refsect2> http://cvs.php.net/viewcvs.cgi/peardoc/en/package/database/mdb2.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/database/mdb2.xml diff -u peardoc/en/package/database/mdb2.xml:1.2 peardoc/en/package/database/mdb2.xml:1.3 --- peardoc/en/package/database/mdb2.xml:1.2 Fri May 5 11:48:52 2006 +++ peardoc/en/package/database/mdb2.xml Fri May 5 14:44:53 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.2 $ --> +<!-- $Revision: 1.3 $ --> <sect1 id="package.database.mdb2"> <title>MDB2</title> <para> @@ -10,7 +10,7 @@ &package.database.db.intro-connect; &package.database.db.intro-query; &package.database.db.intro-fetch; -<!-- &package.database.db.intro-execute; -> + &package.database.db.intro-execute; <!-- &package.database.db.intro-auto; -> &package.database.db.intro-portability; <!-- &package.database.db.intro-sequences; ->
« previous php.pear.doc (#7880) next »