how to catch db error |
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 PHP Code: $this->db->error(); CodeIgniter User Guide - Handling Errors What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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; } }
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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 (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.
Is there an easier way?
why ci function cannot detected if there is a query error PHP Code: if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`')) What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |