我正在尝试创建一个PDO查询构建器类,但它在更新功能中不起作用。我收到错误:
警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配
任何人都可以告诉我如何让它发挥作用。感谢
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();
答案 0 :(得分:-1)
谢谢你们,我自己找到了一个解决方案,想分享我的解决方案。
这是代码更新
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);
}
}
我认为问题可能是 prepare语句不能在单独的函数中使用bindValue()。 execute()的参数也允许我将所有绑定值放入prepare语句中。 最后,问题解决了。