Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter,选择两次或不选择_Php_Database_Codeigniter - Fatal编程技术网

Php codeigniter,选择两次或不选择

Php codeigniter,选择两次或不选择,php,database,codeigniter,Php,Database,Codeigniter,我想知道如果我想在数据库中更新一个spesific用户,是否需要选择(where语句)两次 例如 // Add new income for the day function add_new_income($user_id, $budget_group_id, $day, $month, $year, $income) { // Check if there has been posted anything at the same day $this->db->wh

我想知道如果我想在数据库中更新一个spesific用户,是否需要选择(where语句)两次

例如

// Add new income for the day
function add_new_income($user_id, $budget_group_id, $day, $month, $year, $income)
{

    // Check if there has been posted anything at the same day
    $this->db->where('day',$this->current_date);
    $this->db->where('month',$this->current_month);
    $this->db->where('user_id',$user_id);
    $this->db->where('budget_group_id',$budget_group_id);

    $query = $this->db->get('budget_day',1);

    // Check if something was found
    if($query->num_rows() > 0)
    {
        // If something was found, update the value
        $data = array('income_total' => $income);
        $this->db->update('budget_day',$data);
    }

}

这样行吗?或者我必须运行一个新的“db->where”语句吗?

您需要编写两次where条件,但您可以在单行中尝试,如下所示:

$this->db->where(array('day'=>$this->current_date, 'month'=>$this->current_month, 'user_id'=>$user_id, 'budget_group_id'=>$budget_group_id));

您必须为
update
编写新的
where
条件。最好将所有
where
条件存储在
array
中,并将该
array
用于
选择
update
(如果必须)

$where = array(
    'day' => $this->current_date,
    'month' => $this->current_month,
    'user_id' => $user_id,
    'budget_group_id' => $budget_group_id
);
...
$this->db->where($where);
在您的情况下,似乎不需要两个要求,您只需要运行
update
query。您可以使用
$this->db->infected_rows()
检查是否更新了某些内容