将 PDO exec(array()) 与多个操作数一起使用
Use PDO exec(array()) with several operands
我想在我的代码中保护我的请求。
今天我现在的函数是这样的
public function UpdatePMS($table,$data,$where) {
$ret = array();
$set_data = "";
foreach($data as $key => $value){
$set_data .= $key."= '".$value."', ";
}
if (isset($where)) {
$where = "WHERE ".$where;
}
$sql = "UPDATE ".$table." SET ".$set_data."".$where;
$sql = str_replace(", WHERE", " WHERE", $sql);
$stm = $this->db->prepare($sql);
$ret = $stm->execute();
return $ret;
}
通过这种方式,我可以select任何表格、任何数据和任何条件。
例如:
WHERE id = 1 and status < 10
或仅
WHERE id = 10
或者有时
WHERE id = 1 and status >= 5
where 的内容可以更改。
一种普遍的要求。
同样适用于删除、更新、Select、插入。
我试过这样做,但没有用。
$db = new PDO('mysql:host=localhost;dbname=asterisk','root','');
$table = "my_table";
$where = "WHERE id = 1";
$sql = 'SELECT * FROM :table :where';
$stm = $db->prepare($sql);
$stm->execute(array(":table" => $table, ":where" => $where));
$ret = $stm->fetchall(PDO::FETCH_ASSOC);
有什么想法吗?
坦率地说,您不能以这种方式使用准备好的语句。有规则可循。所以写这样的东西是没有意义的
$table = "my_table";
$where = "WHERE id = 1";
$sql = 'SELECT * FROM :table :where';
$stm = $db->prepare($sql);
$stm->execute(array(":table" => $table, ":where" => $where));
你应该写这段代码
$sql = 'SELECT * FROM my_table WHERE id = ?';
$stm = $db->prepare($sql);
$stm->execute(array($id));
另外,table和字段名不能参数化,所以还是原封不动写好。
so i need to make one function per different requests, right?
老实说-是的。它会让你免于很多头痛。
public function UpdatePMS($data, $id)
{
$data[] = $id;
$sql = "UPDATE table SET f1 = ?, f2 = ? WHERE id = ?";
$stm = $this->db->prepare($sql);
$ret = $stm->execute($data);
return $ret;
}
将像
一样使用
$obj->UpdatePMS([$f1, $f2], $id);
我想在我的代码中保护我的请求。
今天我现在的函数是这样的
public function UpdatePMS($table,$data,$where) {
$ret = array();
$set_data = "";
foreach($data as $key => $value){
$set_data .= $key."= '".$value."', ";
}
if (isset($where)) {
$where = "WHERE ".$where;
}
$sql = "UPDATE ".$table." SET ".$set_data."".$where;
$sql = str_replace(", WHERE", " WHERE", $sql);
$stm = $this->db->prepare($sql);
$ret = $stm->execute();
return $ret;
}
通过这种方式,我可以select任何表格、任何数据和任何条件。 例如:
WHERE id = 1 and status < 10
或仅
WHERE id = 10
或者有时
WHERE id = 1 and status >= 5
where 的内容可以更改。 一种普遍的要求。 同样适用于删除、更新、Select、插入。
我试过这样做,但没有用。
$db = new PDO('mysql:host=localhost;dbname=asterisk','root','');
$table = "my_table";
$where = "WHERE id = 1";
$sql = 'SELECT * FROM :table :where';
$stm = $db->prepare($sql);
$stm->execute(array(":table" => $table, ":where" => $where));
$ret = $stm->fetchall(PDO::FETCH_ASSOC);
有什么想法吗?
坦率地说,您不能以这种方式使用准备好的语句。有规则可循。所以写这样的东西是没有意义的
$table = "my_table";
$where = "WHERE id = 1";
$sql = 'SELECT * FROM :table :where';
$stm = $db->prepare($sql);
$stm->execute(array(":table" => $table, ":where" => $where));
你应该写这段代码
$sql = 'SELECT * FROM my_table WHERE id = ?';
$stm = $db->prepare($sql);
$stm->execute(array($id));
另外,table和字段名不能参数化,所以还是原封不动写好。
so i need to make one function per different requests, right?
老实说-是的。它会让你免于很多头痛。
public function UpdatePMS($data, $id)
{
$data[] = $id;
$sql = "UPDATE table SET f1 = ?, f2 = ? WHERE id = ?";
$stm = $this->db->prepare($sql);
$ret = $stm->execute($data);
return $ret;
}
将像
一样使用$obj->UpdatePMS([$f1, $f2], $id);