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

From: Date: Tue, 30 Mar 2004 00:45:25 +0000
Subject: cvs: peardoc /en/package/database/db intro-auto.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-3137@lists.php.net to get a copy of this message
danielc Mon Mar 29 19:45:25 2004 EDT Modified files: /peardoc/en/package/database/db intro-auto.xml Log: Clean up xml nesting. http://cvs.php.net/diff.php/peardoc/en/package/database/db/intro-auto.xml?r1=1.15&r2=1.16&ty=u Index: peardoc/en/package/database/db/intro-auto.xml diff -u peardoc/en/package/database/db/intro-auto.xml:1.15 peardoc/en/package/database/db/intro-auto.xml:1.16 --- peardoc/en/package/database/db/intro-auto.xml:1.15 Mon Mar 29 19:40:47 2004 +++ peardoc/en/package/database/db/intro-auto.xml Mon Mar 29 19:45:25 2004 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.15 $ --> +<!-- $Revision: 1.16 $ --> <refentry id="package.database.db.intro-auto"> <refnamediv> <refname>Introduction - autoPrepare &amp; autoExecute</refname> @@ -13,52 +13,61 @@ <function>autoPrepare</function></link> and <link linkend="package.database.db.db-common.autoexecute"> <function>autoExecute</function></link> - are facilities. With them, you don't have to write anymore these boring - (insert or update) sql queries which are difficult to maintain when you - add a field for instance. - </para> + are facilities. With them, you don't have to write anymore these boring + (insert or update) sql queries which are difficult to maintain when you + add a field for instance. + </para> + <para> Imagine you have an 'user' table with 3 fields (id, name and country). You have to write sql queries like: - <programlisting role="sql"> - <![CDATA[ + + <programlisting role="sql"> + <![CDATA[ INSERT INTO table (id, name, country) VALUES (?, ?, ?) UPDATE table SET id=?, name=?, country=? WHERE ... - ]]> - </programlisting> + ]]> + </programlisting> + If you add a field ('birthYear' for example), you have to rewrite your queries which is boring and can lead to bugs (if you forget one query for instance). </para> </refsect2> + <refsect2> <title>autoPrepare</title> <para> With <link linkend="package.database.db.db-common.autoprepare"> - <function>autoPrepare</function></link>, you don't have to write your - insert or update queries. For example: - <programlisting role="php"> - <![CDATA[ + <function>autoPrepare</function></link>, you don't have to write your + insert or update queries. For example: + + <programlisting role="php"> + <![CDATA[ // Once you have a valid DB object named $db... $table_name = 'user'; $table_fields = array('id', 'name', 'country'); $sth = $db->autoPrepare($table_name, $table_fields, DB_AUTOQUERY_INSERT); ]]> - </programlisting> + </programlisting> + In this example, <link linkend="package.database.db.db-common.autoprepare"> <function>autoPrepare</function></link> will build the following sql query: - <programlisting role="sql"> - <![CDATA[ + + <programlisting role="sql"> + <![CDATA[ INSERT INTO user (id, name, country) VALUES (?, ?, ?) ]]> - </programlisting> + </programlisting> + And then, it will call <link linkend="package.database.db.db-common.prepare"> <function>prepare</function></link> with it. To add records, you have so to use <link linkend="package.database.db.db-common.execute"> <function>execute</function></link> or <link linkend="package.database.db.db-common.executemultiple"> <function>executeMultiple</function></link> like: - <programlisting role="php"> - <![CDATA[ + + <programlisting role="php"> + <![CDATA[ // Once you have a valid DB object named $db... $table_name = 'user'; $table_fields = array('id', 'name', 'country'); @@ -66,11 +75,13 @@ $sth = $db->autoPrepare($table_name, $table_fields, DB_AUTOQUERY_INSERT); $db->execute($sth, $table_values); ]]> - </programlisting> + </programlisting> + So, you don't have to write any sql insert queries! But it works with update queries too. For flexibility reasons, you have only to write the WHERE statement of the query. For instance: - <programlisting role="php"> + + <programlisting role="php"> <![CDATA[ // Once you have a valid DB object named $db... $table_name = 'user'; @@ -80,22 +91,27 @@ DB_AUTOQUERY_UPDATE, 'id = 1'); $db->execute($sth, $table_values); ]]> - </programlisting> + </programlisting> + <link linkend="package.database.db.db-common.autoprepare"> - <function>autoPrepare</function></link> will build the following query: - <programlisting role="sql"> + <function>autoPrepare</function></link> will build the following query: + + <programlisting role="sql"> <![CDATA[ UPDATE user SET name=?, country=? WHERE id=1 ]]> - </programlisting> + </programlisting> + Then, it will call <link linkend="package.database.db.db-common.prepare"> <function>prepare</function></link> with it. </para> + <para> Be careful, if you don't specify any WHERE statement, all the records of the table will be updated. </para> </refsect2> + <refsect2> <title>autoExecute</title> <para> @@ -105,10 +121,12 @@ <function>autoPrepare</function></link> and <link linkend="package.database.db.db-common.autoprepare"> <function>execute</function></link>. </para> + <para> - You only need an associative array (key => value) where keys are fields names and values are - corresponding values of these fields. For instance: - <programlisting role="php"> + You only need an associative array (key => value) where keys are fields names and values are + corresponding values of these fields. For instance: + + <programlisting role="php"> <![CDATA[ // Once you have a valid DB object named $db... $table_name = 'user'; @@ -119,17 +137,20 @@ ); $db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT); ]]> - </programlisting> + </programlisting> + And that's all! The following query is built and executed: - <programlisting role="sql"> + <programlisting role="sql"> <![CDATA[ INSERT INTO user (id, name, country) VALUES (1, 'Fabien', 'France') ]]> - </programlisting> + </programlisting> + And it's the same thing for update queries: - <programlisting role="php"> + + <programlisting role="php"> <![CDATA[ // Once you have a valid DB object named $db... $table_name = 'user'; @@ -140,21 +161,26 @@ $db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_UPDATE, 'id = 1'); ]]> - </programlisting> + </programlisting> + The following query is built and executed: - <programlisting role="sql"> + + <programlisting role="sql"> <![CDATA[ UPDATE user SET name='Fabien', country='France' WHERE id = 1 ]]> - </programlisting> + </programlisting> </para> + <para> Be careful, if you don't specify any WHERE statement, all the records of the table will be updated. </para> </refsect2> + </refsect1> + </refentry> <!-- Keep this comment at the end of the file Local variables:

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