Welcome Guest, Not a member yet? Register   Sign In
how to catch db error
#1

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
Reply
#2

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 )
Reply
#3

(This post was last modified: 10-12-2019, 07:45 PM by tp45.)

Try

PHP Code:
if($this->db->insert($table,$data)){
  return true;
}else{
  return false;
    
//or
  
echo $this->db->error();

Reply
#4

(This post was last modified: 10-13-2019, 07:29 PM by amansusanto.)

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;
    }
  }
Reply
#5

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 )
Reply
#6

(10-14-2019, 04:21 AM)InsiteFX Wrote: 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.

still cannot, because data entry by many people / user

it could be red inputted simultaneously by several users
Reply
#7

(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.

table last_id
blue_id
yellow_id
green_id

When you create the users record assign the correct id with an increment method.

still cannot, because data entry by many people / user

it could be red inputted simultaneously by several users

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.
Reply
#8

Is there an easier way?

why ci function cannot detected if there is a query error
Reply
#9

PHP Code:
if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`'))
{
        $error $this->db->error(); // Has keys 'code' and 'message'

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB