我试图弄清楚为什么查询返回false,即使它更新数据库就好了。
我有以下内容:
$results = $User->UpdateUser($UpdateAdminId,$UpdateFirstName);
调用以下方法:
function UpdateUser($AdminId,$FirstName)
{
return $this->sdb->dbUpdate("administrators",array("FirstName"=>$FirstName),array("AdminId"=>$AdminId));
}
我可以看到数据库使用正确的信息进行更新。如果我将查询更改为简单选择,则返回true。
但为什么此更新在更新时返回false?
以下是更新方法:
function dbUpdate($table_name,$update_array,$update_condition_array=array())
{
$colums_val="";
$where_condition="";
$this->values=array();
$and_val="";
foreach($update_array as $col => $val)
{
$colums_val=$colums_val."`".trim($col)."`=?,";
$this->values[]=$val;
}
$colums_val=rtrim($colums_val,",");
foreach($update_condition_array as $col => $val)
{
$where_condition=$where_condition.$and_val." `".trim($col)."`=? ";
$this->values[]=$val;
$and_val=$this->and_or_condition;
}
if($where_condition)
$where_condition=" WHERE ".rtrim($where_condition,",");
/* Add Order By condition */
$where_condition=$this->getOrderbyCondition($where_condition);
/* Add Limit condition */
$where_condition=$this->getLimitCondition($where_condition);
try
{
if($this->rollbackTransaction&&$this->beginTransaction)
return;
if($this->beginTransaction==true)
{
if($this->dbh==NULL)
{
$this->dbh = new PDO("mysql:host=$this->host_name;dbname=$this->db_name", $this->user_name, $this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh->beginTransaction();
}
}
else
{
$this->dbh = new PDO("mysql:host=$this->host_name;dbname=$this->db_name", $this->user_name, $this->password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
$this->message_info="Connected to database";
if($this->charset)
$this->dbh->exec("set names ".$this->charset);
$this->query="UPDATE $table_name SET $colums_val $where_condition";
$stmt = $this->dbh->prepare($this->query);
$stmt->execute($this->values);
$this->rows_affected=$stmt->rowCount();
if($this->beginTransaction==false)
$this->dbh = NULL;
if($this->resetAllSettings==true)
$this->resetSettings();
}
catch(PDOException $e)
{
if($this->beginTransaction==true)
{
$this->rollbackTransaction=true;
$this->dbh->rollBack();
}
$this->error_info=$e->getMessage();
}
}