根据user_name - codeigniter获取最后一个id


grabbing the last last id based on user_name - codeigniter

我正在做一个项目,它有一个主要的文章,你可以附加许多不同的文章到它。因此,在提交时,我将文章分成了两个不同的部分,因为我需要它们提交到两个不同的表中。

提交第一部分后,我试图根据他们的user_name获取最后提交的id,以便我可以附加文章的其余部分。如果这说得通的话。

我尝试了几种不同的方法,但似乎没有什么能抓住那个id。

首先,我尝试了insert_id()方法,但返回0

public function pullLastStoryId($author){
    $this->db->where('author', $author);
    $this->db->insert_id();
    $story = $this->db->get('story_tbl');
    return $story->result();    
} 

然后我也试着抓取

public function pullLastStoryId($author){
    $story = $this->db->query('SELECT LAST_INSERT_ID() INTO story_tbl;')
    return $story->result();    
}   

任何想法的

我猜您是在谈论两个表之间的外键关系。这样做的一种方法是:

$this->db->insert('author',$vals);
$id = $this->db->insert_id();
// do something with ID.

据我所知,你可能会这样做:

$story_row = $this->db->get_where('story_tbl',array('id' => $id));

您不应该担心不同的user_name属性,因为这将是最后创建的ID。如果你真的想要回那一行,你可以这样做:

$author_row = $this->db->get_where('author',array('author_id' => $id));

如果您需要在其他地方拥有这个ID(例如,在提交另一个表单之后),您可以使用hidden表单输入,或者(这更安全),您可以将该ID存储在会话中。

我不太明白这个问题。你在找这样的东西吗?

public function pullLastStoryId(){
    $story = $this->db->query('SELECT id from tablename where id = LAST_INSERT_ID()')
    return $story->result();    
}   

public function pullLastStoryId(){
    $id = $this->db->insert_id();
    $this->db->where('id',$id);
    $story = $this->db->get('story_tbl');
    return $story->result();    
} 

如果您想获得Last id,那么您的方法是错误的。$this->db->insert_id()或其他活动查询。SELECT id FROM table ORDER BY id DESC LIMIT 1