3

Using codeigniter I'm trying to insert a row into my database, but I want the data in the row to be generated by MySQL. What is the proper way of doing it?

$data['my_id'] = 'UUID()';
$db->insert('my_table',$data);

Obviously the above won't work, but how can I make it function?

EDIT: Theoretically this is what the above should produce:

INSERT INTO my_table (my_id) VALUES (UUID());

Naturally my actually query isn't that simple, but that should get the point across.

Aram Papazian
  • 2,453
  • 6
  • 38
  • 45
  • you are saying you will retrieve data from Mysql using SQL functions and then you want to insert it in the MySQl again the same data.....am i right if not please correct me..... – Venkata Krishna Dec 12 '12 at 13:06
  • No, I'm not grabbing any data from MySQL. I just want to make 1 call to the server. (i'll update my details to give a little more detail as to what I mean) – Aram Papazian Dec 12 '12 at 13:09
  • 1
    @GBD UUID() is a MySQL function that produces a unique identifier http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid – Aram Papazian Dec 12 '12 at 13:10
  • 1
    `$this->db->set('my_id', 'UUID()', FALSE);` then do the insert. – itachi Dec 12 '12 at 13:11
  • @itachi What if I had more data as well? Say $data['my_amount'] = '$12'; ? (variable number of items in the data array)... Would I have to do a set for each one or can I pass in my array of data and then do a second call for the 'my_id' one? – Aram Papazian Dec 12 '12 at 13:14
  • 1) construct the array 2)`$this->db->set('my_id', 'UUID()', FALSE);` 3) `$db->insert('my_table',$data);` You can use `set` only when you need it. – itachi Dec 12 '12 at 13:18
  • [**this link**](http://stackoverflow.com/questions/6354315/inserting-now-into-database-with-codeigniters-active-record) should clear it. – itachi Dec 12 '12 at 13:20
  • @itachi Awesome! Thanks =) Did you want to put the answer below so I can accept it? =D – Aram Papazian Dec 12 '12 at 13:21

1 Answers1

9

you can use $this->db->set('my_id', 'UUID()', FALSE);

For passing mltiple params,

$data = array(.....) //add the params.
$this->db->set('my_id', 'UUID()', FALSE);
$this->db->insert('my_table',$data);
itachi
  • 6,323
  • 3
  • 30
  • 40
  • This works great. You can skip the `$data` var completely if you're just updating one field. – qwerty Jan 06 '14 at 18:42