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.

8
  • 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..... Commented Dec 12, 2012 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) Commented Dec 12, 2012 at 13:09
  • 1
    @GBD UUID() is a MySQL function that produces a unique identifier dev.mysql.com/doc/refman/5.0/en/… Commented Dec 12, 2012 at 13:10
  • 1
    $this->db->set('my_id', 'UUID()', FALSE); then do the insert. Commented Dec 12, 2012 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? Commented Dec 12, 2012 at 13:14

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This works great. You can skip the $data var completely if you're just updating one field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.