![]() |
how to catch db error - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: how to catch db error (/showthread.php?tid=74585) |
how to catch db error - amansusanto - 10-11-2019 i have insert new row to table in same time ( 10 users ) my primary key is determined from user input with in 3 groups Blue Yellow Green if user select Blue ID must be B001, B002 Yellow Y001, Y002 i have check to get last Id using query but sometimes getting error duplicate key i want to catch db error so i can change ID and try to insert again try{ $result = $this->db->insert($table,$data); echo $result; print $result; } catch(Exception $e) { echo "Test Error" //get new ID // Insert data with new ID } but now working RE: how to catch db error - InsiteFX - 10-12-2019 PHP Code: $this->db->error(); CodeIgniter User Guide - Handling Errors RE: how to catch db error - tp45 - 10-12-2019 Try PHP Code: if($this->db->insert($table,$data)){ RE: how to catch db error - amansusanto - 10-13-2019 not working in controller : $check=false; while (!$check) { $no = $this->m_home->getLastId($grp); $data = array( 'no' => $no, 'usergroup' => $group, 'name' => $n, 'mobile' => $m ); $check= $this->m_home->input_data($data,'users'); } in model function input_data($data,$table) { $this->db->db_debug = false; if($this->db->insert($table,$data)) { return true; } else { return false; } } RE: how to catch db error - InsiteFX - 10-14-2019 A better way would be to create a separate table for holding the last id's. table last_id blue_id yellow_id green_id When you create the users record assign the correct id with an increment method. RE: how to catch db error - amansusanto - 10-14-2019 (10-14-2019, 04:21 AM)InsiteFX Wrote: A better way would be to create a separate table for holding the last id's. still cannot, because data entry by many people / user it could be red inputted simultaneously by several users RE: how to catch db error - Chroma - 10-14-2019 (10-14-2019, 04:42 AM)amansusanto Wrote:(10-14-2019, 04:21 AM)InsiteFX Wrote: A better way would be to create a separate table for holding the last id's. So fundamentally, you have a race condition. The cleanest way to solve this is to use the primary key to store the row in the database, the use a lock to fix up within the transaction the colored tags. We do this when creating invoice numbers, create a second table with the last number in, lock it as part of the transaction and then add a 1 to the stored value. Then at the end of the transaction, update the value in the companion table. This way, you can block other accesses to the critical value, giving you access. when the transaction completes, it is unlocked and the next one can proceed. Be warned, this will not scale to large numbers of users, as at some point, the queued transactions will begin to time out. RE: how to catch db error - amansusanto - 10-15-2019 Is there an easier way? why ci function cannot detected if there is a query error RE: how to catch db error - InsiteFX - 10-16-2019 PHP Code: if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`')) |