PDA

Bekijk Volledige Versie : [PHP] PEAR DB en prepared statements



Tim Stoop
27/12/02, 15:12
Hoi mensen,

Kan iemand me een stukje voorbeeld-code aangeven van hoe ik in PEAR omga met
prepared statements? Hoe maak ik ze aan en hoe gebruik ik ze? Ik vind wel
het commando prepare($query), maar nergens een voorbeeld van hoe ik de
variabelen in de query instel...

Alvast bedankt!

--
Gegroet/Greetings,
Tim

Daniel Tryba
27/12/02, 17:20
Tim Stoop <cvdSP@mmens-informatica.net> wrote:
> Kan iemand me een stukje voorbeeld-code aangeven van hoe ik in PEAR omga met
> prepared statements? Hoe maak ik ze aan en hoe gebruik ik ze? Ik vind wel
> het commando prepare($query), maar nergens een voorbeeld van hoe ik de
> variabelen in de query instel...

Hmmm, het staat toch echt gewoon in de documentatie van PEAR::DB
Na een prepare moet je een execute doen, en _daar_ geef je je parameters
aan mee in de vorm van een array:

http://pear.php.net/manual/en/core.db.prepare.php
<quote>
See "Execute"-section for further information.
</quote>

http://pear.php.net/manual/en/core.db.tut_execute.php
<quote>
Execute/ ExecuteMultiple

After preparing the statement, you can excute the query. This means to
assign the variables to the prepared statement. To do this, execute()
requires two arguments, the statement handle of prepare() 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.
<?php
// Example inserting data
$alldata = array(array(1, 'one', 'en'),
array(2, 'two', 'to'),
array(3, 'three', 'tre'),
array(4, 'four', 'fire'));
$sth = $dbh->prepare("INSERT INTO numbers VALUES(?,?,?)");
foreach ($alldata as $row) {
$dbh->execute($sth, $row);
}
?>

In the example the query is done four times:
INSERT INTO numbers VALUES( '1', 'one', 'en')
INSERT INTO numbers VALUES( '2', 'two', 'to')
INSERT INTO numbers VALUES( '3', 'three', 'tre')
INSERT INTO numbers VALUES( '4', 'four', 'fire')
</quote>

--

Daniel Tryba

Tim Stoop
27/12/02, 17:57
Daniel Tryba wrote:
> Hmmm, het staat toch echt gewoon in de documentatie van PEAR::DB
<knip>

Inderdaad, ik zoek gewoon niet goed genoeg. Bedankt!

--
Gegroet/Greetings,
Tim