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

From: 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&eacute;cuter</refname> @@ -26,16 +26,19 @@ diff&eacute;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&eacute;er un script multi-langue, vous pouvez cr&eacute;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&eacute;rifiez que les param&egrave;tres peuvent &ecirc;tre remplac&eacute;e durant l'ex&eacute;cution du script. Substituez ces param&egrave;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&agrave;! Maintenant que vous avez votre requ&ecirc;te g&eacute;n&eacute;rique n&eacute;cessaire pour @@ -112,25 +118,25 @@ <title>Ins&eacute;rer des donn&eacute;es dans la base de donn&eacute;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&ecirc;te est envoy&eacute;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&eacute;sultat est le m&ecirc;me. Si un des enregistrements &eacute;choue,

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