我试图使一个PDO查询生成器类,但它不工作在更新功能。我得到错误:
警告:PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number:绑定变量的数量与令牌的数量不匹配
有谁能告诉我怎么做吗?由于class query extends PDO {
private $_statement;
private $_prepare;
public function __construct() {
parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
$this->_statement = "";
$this->_prepare = NULL;
}
function UPDATE($table) {
$this->_prepare = $this->prepare("UPDATE " . $table);
$this->_statement = $this->_prepare->queryString;
return $this;
}
function DATA($data) {
ksort($data);
if (strstr($this->_statement, "UPDATE")) {
$fieldDetails = null;
foreach ($data as $key => $value) {
$fieldDetails.="$key = :$key, ";
}
$fieldDetails = rtrim($fieldDetails, ', ');
$this->_prepare = $this->prepare($this->_statement . " SET " . $fieldDetails);
foreach ($data as $key => $value) {
$this->_prepare->bindValue(":$key", $value);
}
}
$this->_statement = $this->_prepare->queryString;
return $this;
}
function WHERE($where, $operator = "AND") {
$condition = null;
if (is_array($where)) {
if (count($where) != 0) {
foreach ($where as $key => $value) {
$condition.="$key = :$tempK " . $operator . " ";
}
}
}
$condition = rtrim($condition, $operator . " ");
$this->_prepare = $this->prepare($this->_statement . " WHERE " . $condition);
foreach ($where as $key => $value) {
$this->_prepare->bindValue(":$tempK", $value);
}
$this->_statement = $this->_prepare->queryString;
return $this;
}
function send() {
return $this->_prepare->execute();
}
}
这是测试函数
$q = new query();
$q->UPDATE("user")
->DATA(array("email" => "dd"))
->WHERE(array("username"=>"ee"))
->send();
谢谢大家,我自己找到了一个解决方案,想分享一下我的解决方案。
这是更新后的代码
class query extends PDO {
private $_statement;
private $_prepare;
private $_values;
public function __construct() {
parent::__construct(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
$this->_statement = "";
$this->_prepare = NULL;
$this->_values = array();
}
function UPDATE($table) {
$this->_prepare = $this->prepare("UPDATE " . $table);
$this->_statement = $this->_prepare->queryString;
return $this;
}
function DATA($data) {
ksort($data);
if (strstr($this->_statement, "UPDATE")) {
$fieldDetails = null;
foreach ($data as $key => $value) {
$fieldDetails.="$key = :$key, ";
}
$fieldDetails = rtrim($fieldDetails, ', ');
$this->_prepare = $this->prepare($this->_statement . " SET " . $fieldDetails);
foreach ($data as $key => $value) {
$this->_values[":$key"] = $value;
}
}
$this->_statement = $this->_prepare->queryString;
return $this;
}
function WHERE($where, $operator = "AND") {
$condition = null;
if (is_array($where)) {
if (count($where) != 0) {
foreach ($where as $key => $value) {
$condition.="$key = :where$key " . $operator . " ";
}
}
}
$condition = rtrim($condition, $operator . " ");
$this->_prepare = $this->prepare($this->_statement . " WHERE " . $condition);
foreach ($where as $key => $value) {
$this->_values[":where$key"] = $value;
}
$this->_statement = $this->_prepare->queryString;
return $this;
}
function send() {
return $this->_prepare->execute($this->_values);
}
}
我认为问题可能是准备语句不能在单独的函数中使用bindValue()。此外,execute()的参数允许我将所有绑定值放入prepare语句中。最后,问题解决了。