函数调用有什么问题

时间:2016-06-17 23:54:44

标签: php mysql

我在dp.php中有2个php文件(function.php,dp.php)我调用更新函数如下

include('function.php');
$db = new Database();
$db->connect();
$db->update('classes', array('level'=> '(CASE WHEN level = 1 THEN 2
                WHEN level = 2 THEN 3
                WHEN level = 3 THEN 4                    
                END)','modifiedTime'=>date("Y-m-d H:i:s")), 'level IN (1,2,3)');
$res = $db->getResult();
print_r($res);

和更新功能:

     public function update($table,$params=array(),$where){
    if($this->tableExists($table)){
        $args=array();
        foreach($params as $field=>$value){
            $args[]=$field.'="'.$value.'"';
        }
        $sql='UPDATE '.$table.' SET '.implode(',',$args).' WHERE '.$where;          
        $this->myQuery = $sql; // Pass back the SQL
        if($query = $this->myconn->query($sql)){
            array_push($this->result,$this->myconn->affected_rows);
            return true; 
        }else{
            array_push($this->result,$this->myconn->error);
            return false;
        }
    }else{
        return false;
    }
}

我总得到的结果是0,它应该是2,3或4。

执行phpmyadmin时的实际语句是:

    update classes set level = (CASE WHEN level = 1 THEN 2
                WHEN level = 2 THEN 3
                WHEN level = 3 THEN 4                    
                END), modifiedTime = date("Y-m-d H:i:s")) WHERE level IN (1,2,3));

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用current_timestamp而不是在db.php中使用date(..)

    include('function.php');
    $db = new Database();
     $db->connect();
     $db->update('tbl_class', array('level'=> '(CASE WHEN level = 1 THEN 2
                WHEN level = 2 THEN 3
                WHEN level = 3 THEN 4                    
                END)','update_time' => CURRENT_TIMESTAMP), 'level IN (1,2,3)');
     $res = $db->getResult();
     print_r($res);

还有一件事要纠正你的功能

        public function update($table,$params=array(),$where){
        if($this->tableExists($table)){
        $args=array();
        foreach($params as $field=>$value){
            $args[]=$field.' = '.$value.' ';
        }
        $sql='UPDATE '.$table.' SET '.implode(' , ',$args).' WHERE '.$where;
        $this->myQuery = $sql; 
        if($query = $this->myconn->query($sql)){
            array_push($this->result,$this->myconn->affected_rows);
            return true; 
        }else{
            array_push($this->result,$this->myconn->error);
            return false; 
        }
    }else{
        return false; 
    }
}