cvs: peardoc /fr/package/database/db intro-execute.xml
| From: | Daniel Convissor | Date: | Sun, 22 Feb 2004 03:42:04 +0000 |
| Subject: | cvs: peardoc /fr/package/database/db intro-execute.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-2670@lists.php.net to get a copy of this message | ||
danielc Sat Feb 21 22:42:04 2004 EDT
Modified files:
/peardoc/fr/package/database/db intro-execute.xml
Log:
Just synching examples for now.
http://cvs.php.net/diff.php/peardoc/fr/package/database/db/intro-execute.xml?r1=1.2&r2=1.3&ty=u
Index: peardoc/fr/package/database/db/intro-execute.xml
diff -u peardoc/fr/package/database/db/intro-execute.xml:1.2
peardoc/fr/package/database/db/intro-execute.xml:1.3
--- peardoc/fr/package/database/db/intro-execute.xml:1.2 Wed Oct 29 22:12:17 2003
+++ peardoc/fr/package/database/db/intro-execute.xml Sat Feb 21 22:42:04 2004
@@ -1,5 +1,5 @@
-<!-- $Revision: 1.2 $ -->
-<!-- EN-Revision: 1.5 Maintainer: ThomasMoulard Status: ready -->
+<!-- $Revision: 1.3 $ -->
+<!-- EN-Revision: 1.14 Maintainer: ThomasMoulard Status: EXAMPLES_ONLY -->
<refentry id="package.database.db.intro-execute">
<refnamediv>
<refname>Introduction - Exécuter</refname>
@@ -26,16 +26,19 @@
différentes pour INSERT:
<programlisting>
<![CDATA[
-db1 : INSERT INTO tbl_name ( col1, col2 ... ) VALUES ( expr1, expr2 ... )
-db2 : INSERT INTO tbl_name SET col1=expr1, col2=expr2 ...
+db1: INSERT INTO tbl_name (col1, col2) VALUES (expr1, expr2)
+db2: INSERT INTO tbl_name SET col1=expr1, col2=expr2
]]>
</programlisting>
Au lieu de créer un script multi-langue, vous pouvez créer
un tableau comme celui-ci:
<programlisting role="php">
<![CDATA[
-$statement['db1']['INSERT_PERSON'] = "INSERT INTO person ( surnom, nom,
age ) VALUES ( ?, ?, ? )" ;
-$statement['db2']['INSERT_PERSON'] = "INSERT INTO person SET surnom=?,
nom=?, age=?" ;
+$statement['db1']['INSERT_PERSON'] = 'INSERT INTO person
+ (surnom, nom, age) VALUES (?, ?, ?)';
+
+$statement['db2']['INSERT_PERSON'] = 'INSERT INTO person
+ SET surnom=?, nom=?, age=?';
]]>
</programlisting>
</para>
@@ -58,15 +61,18 @@
comme d'habitude, par ex.:
<programlisting>
<![CDATA[
-SELECT surnom, nom, age FROM personne
- WHERE nom = 'name_a_trouver' AND age < 'limite_d_age'
+SELECT surnom, nom, age
+ FROM personne
+ WHERE nom = 'name_a_trouver' AND age < 'limite_d_age'
]]>
</programlisting>
Maintenant vérifiez que les paramètres peuvent être
remplacée durant l'exécution du
script. Substituez ces paramètres par un marqueur.
<programlisting>
<![CDATA[
-SELECT surnom, nom, age FROM personne WHERE nom = ? AND age < ?
+SELECT surnom, nom, age
+ FROM personne
+ WHERE nom = ? AND age < ?
]]>
</programlisting>
Et voilà! Maintenant que vous avez votre requête
générique nécessaire pour
@@ -112,25 +118,25 @@
<title>Insérer des données dans la base de
donnée</title>
<programlisting role="php">
<![CDATA[
-...
-$alldata = array( array(1, 'one', 'en'),
- array(2, 'two', 'to'),
- array(3, 'three', 'tre'),
- array(4, 'four', 'fire'));
-$sth = $dbh->prepare("INSERT INTO nombres VALUES(?,?,?)");
+// Once you have a valid DB object named $db...
+$alldata = array(array(1, 'one', 'en'),
+ array(2, 'two', 'to'),
+ array(3, 'three', 'tre'),
+ array(4, 'four', 'fire'));
+$sth = $db->prepare('INSERT INTO nombres VALUES (?, ?, ?)');
foreach ($alldata as $row) {
- $dbh->execute($sth, $row);
+ $db->execute($sth, $row);
}
-]]>
+ ]]>
</programlisting>
</example>
Dans cette exemple, la requête est envoyée quatre fois:
<programlisting>
<![CDATA[
-INSERT INTO nombres VALUES( '1', 'one', 'en')
-INSERT INTO nombres VALUES( '2', 'two', 'to')
-INSERT INTO nombres VALUES( '3', 'three', 'tre')
-INSERT INTO nombres VALUES( '4', 'four', 'fire')
+INSERT INTO nombres VALUES ('1', 'one', 'en')
+INSERT INTO nombres VALUES ('2', 'two', 'to')
+INSERT INTO nombres VALUES ('3', 'three', 'tre')
+INSERT INTO nombres VALUES ('4', 'four', 'fire')
]]>
</programlisting>
<link linkend="package.database.db.db-common.executemultiple">
@@ -144,15 +150,14 @@
</title>
<programlisting role="php">
<![CDATA[
-...
-$alldata = array( array(1, 'one', 'en'),
- array(2, 'two', 'to'),
- array(3, 'three', 'tre'),
- array(4, 'four', 'fire'));
-$sth = $dbh->prepare("INSERT INTO nombres VALUES(?,?,?)");
-$dbh->executeMultiple($sth, $alldata);
-}
-]]>
+// Once you have a valid DB object named $db...
+$alldata = array(array(1, 'one', 'en'),
+ array(2, 'two', 'to'),
+ array(3, 'three', 'tre'),
+ array(4, 'four', 'fire'));
+$sth = $db->prepare('INSERT INTO nombres VALUES (?, ?, ?)');
+$db->executeMultiple($sth, $alldata);
+ ]]>
</programlisting>
</example>
Le résultat est le même. Si un des enregistrements échoue,