锟杰讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷库都支锟斤拷预锟斤拷锟斤拷锟斤拷锟侥革拷锟筋。什么锟斤拷预锟斤拷锟斤拷锟斤拷洌匡拷锟斤拷园锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭拷锟斤拷械锟� SQL 锟斤拷一锟街憋拷锟斤拷锟斤拷锟侥o拷澹拷锟斤拷锟斤拷锟绞癸拷帽锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷卸锟斤拷啤锟皆わ拷锟斤拷锟斤拷锟斤拷锟斤拷源锟斤拷锟斤拷锟斤拷锟矫达拷锟斤拷
预锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷唯一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷支锟街碉拷时PDO 锟斤拷模锟解处锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷确锟斤拷锟斤拷锟斤拷锟斤拷锟捷匡拷锟角凤拷锟斤拷锟斤拷锟斤拷锟斤拷墓锟斤拷埽锟斤拷锟斤拷锟斤拷锟饺凤拷锟接︼拷贸锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷同锟斤拷锟斤拷锟捷凤拷锟斤拷模式锟斤拷
Example #1 锟斤拷预锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截革拷锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷 name 锟斤拷 value 锟斤拷锟斤拷锟接︼拷锟斤拷锟斤拷锟秸嘉伙拷锟斤拷锟街达拷锟揭伙拷锟斤拷锟斤拷锟斤拷询
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// 锟斤拷锟斤拷一锟斤拷
$name = 'one';
$value = 1;
$stmt->execute();
// 锟矫诧拷同锟斤拷值锟斤拷锟斤拷锟斤拷一锟斤拷
$name = 'two';
$value = 2;
$stmt->execute();
?>
Example #2 锟斤拷预锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截革拷锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷 name 锟斤拷 value 取锟斤拷 ? 占位锟斤拷锟斤拷位锟斤拷锟斤拷执锟斤拷一锟斤拷锟斤拷锟斤拷锟窖拷锟�
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// 锟斤拷锟斤拷一锟斤拷
$name = 'one';
$value = 1;
$stmt->execute();
// 锟矫诧拷同锟斤拷值锟斤拷锟斤拷锟斤拷一锟斤拷
$name = 'two';
$value = 2;
$stmt->execute();
?>
Example #3 使锟斤拷预锟斤拷锟斤拷锟斤拷锟斤拷取锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟接伙拷取锟斤拷锟捷伙拷锟节硷拷值锟斤拷锟结供锟斤拷锟斤拷式锟斤拷锟矫伙拷锟斤拷锟斤拷锟诫被锟皆讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷瞬锟斤拷锟斤拷锟� SQL 注锟诫攻锟斤拷锟斤拷危锟秸★拷
<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
?>
锟斤拷锟斤拷锟斤拷菘锟斤拷锟斤拷锟街э拷郑锟接︼拷贸锟斤拷蚧箍锟斤拷园锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�.锟斤拷锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷诖哟娲拷锟斤拷袒锟饺≈碉拷锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭拷锟轿拷锟斤拷锟揭恍╋拷锟斤拷锟轿拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷知锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥筹拷锟饺★拷锟斤拷锟轿拷锟斤拷锟斤拷蠖ǖ锟街碉拷锟斤拷诮锟斤拷锟侥筹拷锟饺o拷锟酵伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟�
Example #4 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷么娲拷锟斤拷锟�
<?php
$stmt = $dbh->prepare("CALL sp_returns_string(?)");
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000);
// 锟斤拷锟矫存储锟斤拷锟斤拷
$stmt->execute();
print "procedure returned $return_value\n";
?>
锟斤拷锟斤拷锟斤拷指锟斤拷同时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟侥诧拷锟斤拷锟斤拷锟斤拷锟斤法锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷校锟斤拷址锟斤拷锟�"hello"锟斤拷锟斤拷锟捷革拷锟芥储锟斤拷锟教o拷锟斤拷锟芥储锟斤拷锟教凤拷锟斤拷时锟斤拷hello 锟斤拷锟芥换为锟矫存储锟斤拷锟教凤拷锟截碉拷值锟斤拷
Example #5 锟斤拷锟斤拷锟斤拷/锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷么娲拷锟斤拷锟�
<?php
$stmt = $dbh->prepare("CALL sp_takes_string_returns_string(?)");
$value = 'hello';
$stmt->bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);
// 锟斤拷锟矫存储锟斤拷锟斤拷
$stmt->execute();
print "procedure returned $value\n";
?>
Example #6 占位锟斤拷锟斤拷锟斤拷效使锟斤拷
<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name LIKE '%?%'");
$stmt->execute(array($_GET['name']));
// 占位锟斤拷锟斤拷锟诫被锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷位锟斤拷
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name LIKE ?");
$stmt->execute(array("%$_GET[name]%"));
?>