我有一个考勤表,如果不存在,我想在其中插入数据,如果存在则要更新,Loop中的查询工作正常,但花费太多时间。因此,为了减少时间,我将所有查询连接为以分号分隔的字符串。在phpMyAdmin中进行级联工作后获得结果,但在Codeigniter中则没有, 任何猜测为什么? 提前致谢。
function set_attendance()
{
$today = date('Y-m-d');
$data = $this->input->post('member');
$str='';
foreach ($data as $value)
{
$query = $this->db->select('*')
->from('attendance')
->where('user_id',$value['user_id'])
->where('date',$today)
->get();
if($query->num_rows()>0)
{
$obj = array(
'attendance' => $value['status'],
'leavetype' => $value['leavetype'],
);
$where = "user_id = '{$value['user_id']}' AND date = '{$today}'";
$str .= $this->db->update_string('attendance', $obj, $where);
$str.=';';
}
else
{
$obj = array(
's-no' => null,
'user_id' => $value['user_id'],
'date' => $today,
'regby'=>'t',
'leavetype' => $value['leavetype'],
'attendance' => $value['status'],
);
$str .= $this->db->insert_string('attendance', $obj);
$str.=';';
}
}
$this->db->query($str);
$output = array('result'=>true);
return $output ;
}
答案 0 :(得分:0)
好,我得到了答案 只需运行php native multi_query()函数
function set_attendance()
{
$today = date('Y-m-d');
$data = $this->input->post('member');
$str='';
foreach ($data as $value)
{
$query = $this->db->select('*')
->from('attendance')
->where('user_id',$value['user_id'])
->where('date',$today)
->get();
if($query->num_rows()>0)
{
$obj = array(
'attendance' => $value['status'],
'leavetype' => $value['leavetype'],
);
$where = "user_id = '{$value['user_id']}' AND date = '{$today}'";
$str .= $this->db->update_string('attendance', $obj, $where);
$str.=';';
}
else
{
$obj = array(
's-no' => null,
'user_id' => $value['user_id'],
'date' => $today,
'regby'=>'t',
'leavetype' => $value['leavetype'],
'attendance' => $value['status'],
);
$str .= $this->db->insert_string('attendance', $obj);
$str.=';';
}
}
$link = mysqli_connect('localhost','root','','yoga_panel');
if(mysqli_multi_query($link,$str))
{
$output=array('result'=>true);
return $output;
}
}