php pdo参数化,php – 如何正确地使用PDO对象的参数化SELECT查询
您選擇這樣的數(shù)據(jù):
$db = new PDO("...");
$statement = $db->prepare("select id from some_table where name = :name");
$statement->execute(array(':name' => "Jimbo"));
$row = $statement->fetch(); // Use fetchAll() if you want all results, or just iterate over the statement, since it implements Iterator
您以相同的方式插入:
$statement = $db->prepare("insert into some_other_table (some_id) values (:some_id)");
$statement->execute(array(':some_id' => $row['id']));
我建議您配置PDO在錯(cuò)誤時(shí)拋出異常。如果任何查詢失敗,您將獲得PDOException – 無需顯式檢查。要打開異常,請(qǐng)?jiān)趧?chuàng)建$ db對(duì)象后調(diào)用此異常:
$db = new PDO("...");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
總結(jié)
以上是生活随笔為你收集整理的php pdo参数化,php – 如何正确地使用PDO对象的参数化SELECT查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 固态硬盘的优点和缺点是什么
- 下一篇: php验证百度云doc,百度云推送通知埋