1

To begin with, I'm aware to the:

$this->db->insert_id();

With that I will get the id of the last inserted mysql value, but I didn't manage to find any info about the way to retrieve the last inserted value to another colomn, let's say we have a field with name weight, I will get the id with insert_id() but how to get the weight value without the need to write a SQL statement?

I was reading the documentation and only thing I can maybe use is the last_query() method, but I'm not that sure if that will work, is there anything similar?

9
  • 1
    this is one of the million reasons people use data access objects (DAO) these days.. CI is very basic... Commented Feb 22, 2017 at 9:38
  • last_query() will only return the last query string, not it's result (e.g. SELECT * FROM sometable....). Maybe someone else can think of a way, but my guess you'll just have make another query of the db. Commented Feb 22, 2017 at 9:44
  • @Pacio yes, that is what I'm thinking, but was the only thing kinda related that I found, Gogol I'm aware of that, but I was thinking since they have insert_id() method, it should be something similar to get another value. Commented Feb 22, 2017 at 9:48
  • possible duplicate of stackoverflow.com/questions/14433251/… Commented Feb 22, 2017 at 9:57
  • @Diksha that is from 2013, on that year we didn't had codeigniter 3, and also I can't see any valid answer there, except some SQL query's! Commented Feb 22, 2017 at 10:03

1 Answer 1

1

You can also get last record by using

$id = $this->db->insert_id();
$query = $this->db->get_where('your_table_name', array('your_id_field' => $id));
return $query->row();
Sign up to request clarification or add additional context in comments.

4 Comments

that is not the answer to this question. It basically means that the system needs to query the db again.
This is the only way by which you can get what you want
yup agreed @dhruv
Ok, I will accept this answer since there isn't another way around! Thank you!

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.