查询构建器选择数据代码点火器

时间:2019-06-09 14:09:25

标签: php codeigniter

我想问一下在Codeigniter中选择数据的问题。我确实在flashdata中写了“没有注册:”。我必须在此代码中包括哪些查询才能显示数据库中的ID,以便它可以

  

“恭喜!您的数据已注册,没有注册:1”

$this->session->set_flashdata('message', '<div class="alert alert-primary" role="alert">
    Congratulation! Your data has been registered with No Registration:'. $id .' </div>');
redirect('User');

Screenshot of app

1 个答案:

答案 0 :(得分:0)

CodeIgniter具有一个insert_id()函数,该函数返回刚刚插入的项的ID。但是,只有在insert函数之后立即调用它,它才起作用。     

// Insert your data into the table
$this->db->insert('users', [
    'name' => 'John Doe',
    'age' => 50
]);

// Immediately get the id.
$id = $this->db->insert_id();

// Set your feedback message
$this->session->set_flashdata('message', ' Congratulation! Your data has been registered with No Registration:'. $id .' ');

否则,如果您在几个查询之前添加了数据,则可以假设ID为自动递增,尝试获取最后插入的项目的ID。

PS。仅在确定已将项目添加到数据库中时,才使用此解决方案。

// Count all rows in the table
$count = $this->db->count_all('users');
// Limit result to 1 row using the count as an offset
$this->db->limit(1, $count-1);
$data = $this->db->get('users')->result();

$id = empty($data) ? null : $data[0]->id;

// Set your feedback message
$this->session->set_flashdata('message', ' Congratulation! Your data has been registered with No Registration:'. $id .' ');

如果您需要更多帮助,请编辑您的问题,然后在尝试获取ID的地方提交代码,以便我更好地帮助您。