Getting last id from database - true sintax |
[eluser]someone [/eluser]
Hello, I'm proggraming some page with CI, but I'm not sure the sintax when calling last id from database is right. So this is the piece of code for which I'm not sure that's right: $this->GetDB->post_get_id->last_row()->id_post; GetDB is model post_get_id is function id_post is field in database So is this right? Thanks for help! :-)
[eluser]InsiteFX[/eluser]
Code: $data = array(
[eluser]someone [/eluser]
I don't think that. but thanks anyway - it looks I don't set question in right way. So I have function which first insert something into database and then (with function which I paste here) get last ID from database and redirect to editing inserted thing. So in code this looks like: Code: public function something($id = NULL) Hope you will understand me now. Thanks in advance! :-)
[eluser]CroNiX[/eluser]
If you insert something, and use $this->db->insert_id(), it returns the new id (what you are calling "last id") of what you just inserted, which is what InsiteFX was showing you. So all you would need is: Code: $this->InsertDB->functoinsert(); // this called model function insert data in database Personally I would have your functionsert() method return the inserted id, or FALSE on error, so all you would need is: Code: $lastpostid = $this->InsertDB->functoinsert();
[eluser]someone [/eluser]
It works :-) I have new question now. Code: $lastpostid = $this->InsertDB->functoinsert(); If is there an error and data is not inserted to database, can script (which is in use - in this case mysql) return error or is this on me to set flashdata and then set message? Thanks! :-)
[eluser]InsiteFX[/eluser]
Yes you will need to set your own errors the active record will not return mysql errors. See this for displaying errors: CodeIgniter User Guide - Error Handling |
Welcome Guest, Not a member yet? Register Sign In |