首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$this->db->insert_id();在codeigniter中每次返回0

$this->db->insert_id();在codeigniter中每次返回0
EN

Stack Overflow用户
提问于 2015-09-18 23:26:24
回答 2查看 11K关注 0票数 5

我使用codeigniter,一次插入一条记录。但问题是$this->db->insert_id();每次都返回0,但是记录创建成功了。我想不通了。这是常见的情况还是我在犯一些愚蠢的错误。我使用email_id作为主键,使用mobile作为唯一键。

以下是我的code代码:

代码语言:javascript
复制
function signup_insert($data) {

        $result = $this->db->insert('registration_detail', $data);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

这是我的控制器代码:

代码语言:javascript
复制
function insert_signup() {
        $email = $this->input->get('email');
        $name = $this->input->get('name');
        $password1 = $this->input->get('password1');
        $password2 = $this->input->get('password2');
        $phone = $this->input->get('phone');
        $country = $this->input->get('country');

        $data = array(
            'email' => $email,
            'name' => $name,
            'pwd' => $password1,
            'phone' => $phone,
            'country' => $country
        );

        $insert_id = $this->signup->signup_insert($data);
        print_r($insert_id);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-18 23:44:53

因此使用$this->db->insert_id()

执行数据库插入时,

插入ID号。

并且您的数据库中没有自动递增的id。

检查数据是否已插入或未使用

代码语言:javascript
复制
$this->db->affected_rows()

在执行“写”类型查询(插入、更新等)时,

会显示受影响的行数。

模型

代码语言:javascript
复制
function signup_insert($data) {
    $result = $this->db->insert('registration_detail', $data);
    $rows =  $this->db->affected_rows();
    if($rows>0){
    return $rows;
    }else{
      return FALSE;
    }
}

阅读http://www.codeigniter.com/user_guide/database/helpers.html

票数 13
EN

Stack Overflow用户

发布于 2015-09-18 23:43:57

要返回一个id it,应该有一个AUTO_INCREMENT列。如果不是,则无法返回insert_id,请参阅此处的mysql文档。

https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32655723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档