1

I am trying to figure out why a query returns false even though it updates the database just fine.

I have the following:

 $results = $User->UpdateUser($UpdateAdminId,$UpdateFirstName);

which calls the following method:

 function UpdateUser($AdminId,$FirstName)
 {
 return $this->sdb->dbUpdate("administrators",array("FirstName"=>$FirstName),array("AdminId"=>$AdminId));
 }

I can see that the database is updated with the correct info. And if I change the query to be a simple select, it returns true.

But why is this update returning false when it updates?

Here is the update method:

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();
    }
}
4

0 回答 0