php - what is wrong with the function call

564

i have 2 php file (function.php, dp.php) in dp.php i call update function as follow

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);

and the update function :

     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;
    }
}

the result i always get is 0 ,where it should be 2, 3, or 4.

the actual statement the is working when it is executed phpmyadmin is:

    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));

thanks in advance

156

Answer

Solution:

you can try to use current_timestamp instead of using date(..) in your db.php

    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);

one more thing to do is to correct you function

        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; 
    }
}

People are also looking for solutions to the problem: PHP: how to merge one associative array into a sub-array of another multidimensional array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.