Welcome Guest, Not a member yet? Register   Sign In
Getting last id from database - true sintax
#1

[eluser]someone Smile[/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! :-)
#2

[eluser]InsiteFX[/eluser]
Code:
$data = array(
    'title' => 'My title' ,
    'name' => 'My Name' ,
    'date' => 'My date'
);

$this->db->insert('mytable', $data);

return $this->db->insert_id();
#3

[eluser]someone Smile[/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)
{
if ($id == NULL)
{
  //other code to do something
  $this->InsertDB->functoinsert(); // this called model function insert data in database
  // after that the script must get last ID from database
  $datafromdb = $this->GetDB->post_get_id(); // this model function get data from database
  $lastrowdata = $datafromdb->last_row(); // get last row of field
  $lastpostid = $lastrowdata->id_post(); // get last row of id_post field
  // So there is my question if can I instead all of this code just use following:
  // $this->GetDB->post_get_id()->last_row()->id_post();
  redirect('function/something/' . $lastpostid); // redirect to same function with id
}
else
{
  // do something with data from this post
}
}

Hope you will understand me now. Thanks in advance! :-)
#4

[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
// after that the script must get last ID from database
$lastpostid = $this->db->insert_id();
redirect('function/something/' . $lastpostid); // redirect to same function with id

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();
if($lastpostid !== FALSE)
{
  redirect('function/something/' . $lastpostid);
}
else
{
  //error
}
#5

[eluser]someone Smile[/eluser]
It works :-)

I have new question now.

Code:
$lastpostid = $this->InsertDB->functoinsert();
if($lastpostid !== FALSE)
{
  redirect('function/something/' . $lastpostid);
}
else
{
  //error
}

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! :-)
#6

[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




Theme © iAndrew 2016 - Forum software by © MyBB