【发布时间】:2010-01-27 21:17:32
【问题描述】:
我正在为mysql 使用PDO 连接,我想对我用来检查tags 是否存在于数据库中的查询提出一些意见,并在它的情况下添加它不是。
// the tags are allready processed in $tags array
$check_stmt = $connection->prepare ("SELECT * FROM tags WHERE tag_name = :tag_name");
$save_stmt = $connection->prepare ("INSERT INTO tags (tag_name) VALUES (:tag_name)");
foreach ($tags as $current_tag) {
$check_stmt->bindParam (':tag_name', $current_tag, PDO::PARAM_STR, 32);
$save_stmt->bindParam (':tag_name', $current_tag, PDO::PARAM_STR, 32);
$check_stmt->execute ($current_tag);
if ($check_stmt->rowCount() == 0) $save_stmt->execute ($current_tag);
}
我不熟悉数据库,所以我不确定查询是否得到很好的预测
【问题讨论】: