This is my first php program!



Download 6,9 Mb.
Pdf ko'rish
bet57/69
Sana26.02.2022
Hajmi6,9 Mb.
#471647
1   ...   53   54   55   56   57   58   59   60   ...   69
Bog'liq
PHP

198
|
Chapter 8: Databases
Placeholders
Just as
printf( )
builds a string by inserting values into a template, the PEAR DB can
build a query by inserting values into a template. Pass the
query( )
function SQL with
?
in place of specific values, and add a second parameter consisting of the array of
values to insert into the SQL:
$result = $db->query(
SQL
,
values
);
For example, this code inserts three entries into the
movies
table:
$movies = array(array('Dr No', 1962),
array('Goldfinger', 1965),
array('Thunderball', 1965));
foreach ($movies as $movie) {
$db->query('INSERT INTO movies (title,year) VALUES (?,?)', $movie);
}
There are three characters that you can use as placeholder values in an SQL query:
?
A string or number, which will be quoted if necessary (recommended)
|
A string or number, which will never be quoted
&
A filename, the contents of which will be included in the statement (e.g., for
storing an image file in a BLOB field)
Prepare/Execute
When issuing the same query repeatedly, it can be more efficient to compile the
query once and then execute it multiple times, using the
prepare( )
,
execute( )
, and
executeMultiple( )
methods.
The first step is to call
prepare( )
on the query:
$compiled = $db->prepare(
SQL
);
This returns a compiled query object. The
execute( )
method fills in any placehold-
ers in the query and sends it to the RDBMS:
$response = $db->execute(
compiled
,
values
);
The
values
array contains the values for the placeholders in the query. The return
value is either a query response object, or
DB_ERROR
if an error occurred.
For example, we could insert multiple values into the
movies
table like this:
$movies = array(array('Dr No', 1962),
array('Goldfinger', 1965),
array('Thunderball', 1965));
$compiled = $q->prepare('INSERT INTO movies (title,year) VALUES (?,?)');
foreach ($movies as $movie) {
$db->execute($compiled, $movie);
}
,ch08.16110 Page 198 Wednesday, March 13, 2002 11:44 AM


This is the Title of the Book, eMatter Edition
Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Download 6,9 Mb.

Do'stlaringiz bilan baham:
1   ...   53   54   55   56   57   58   59   60   ...   69




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish