【发布时间】:2011-01-28 21:15:46
【问题描述】:
我正在尝试做一些 PDO CRUD 来学习一些 PDO。我有一个关于 bindParam 的问题。这是我现在的更新方法:
public static function update($conditions = array(), $data = array(), $table = '')
{
self::instance();
// Late static bindings (PHP 5.3)
$table = ($table === '') ? self::table() : $table;
// Check which data array we want to use
$values = (empty($data)) ? self::$_fields : $data;
$sql = "UPDATE $table SET ";
foreach ($values as $f => $v)
{
$sql .= "$f = ?, ";
}
// let's build the conditions
self::build_conditions($conditions);
// fix our WHERE, AND, OR, LIKE conditions
$extra = self::$condition_string;
// querystring
$sql = rtrim($sql, ', ') . $extra;
// let's merge the arrays into on
$v_val = array_values($values);
$c_val = array_values($conditions);
$array = array_merge($v_val, self::$condition_array);
$stmt = self::$db->prepare($sql);
return $stmt->execute($array);
}
在我的“self::$condition_array”中,我从 ? 中获得了所有正确的值。所以查询看起来像这样:
UPDATE table SET this = ?, another = ? WHERE title = ? AND time = ?
如您所见,我不使用 bindParams 而是将正确的值以正确的顺序 ($array) 直接传递给 execute($array) 方法。这就像一个魅力但是不在这里使用 bindParam 是否安全?
如果没有,那我该怎么办?
来自瑞典的感谢
托比亚斯
【问题讨论】: