用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4922|回复: 9
收起左侧

[已解决] 在Model中插入一条记录,如何返回其ID?

[复制链接]
发表于 2010-12-20 09:30:29 | 显示全部楼层 |阅读模式
PHP复制代码
function add_site_url() {
        $now = date("Y-m-d H:i:s");
        $data = array(
            'site_url'      => $this->input->xss_clean($this->input->post('site_url')),
            'ipaddress'     => $this->input->ip_address(),
            'created_at'    => $now
        );
 
        return $this->db->insert('s_site_url', $data);
    }
 
复制代码




想问一下各位朋友,
在这个方法中,怎么将我新插入的这条数据的ID或是整条记录返回?
如果能返回整条记录就好了,不能就退而求其次,取到ID也行?
能取到么?
发表于 2010-12-20 11:00:45 | 显示全部楼层
 楼主| 发表于 2010-12-20 14:07:02 | 显示全部楼层
回复 2# jeongee


  这个在多线程的情况下会出问题么?
发表于 2010-12-20 15:50:50 | 显示全部楼层
我的还是自己写的函数,然后获取!!
发表于 2010-12-20 15:56:03 | 显示全部楼层
回复 3# puras


   不会吧,这个函数其实就是mysql_insert_id();的包装
发表于 2010-12-20 16:49:50 | 显示全部楼层
同意版主的观点!!
 楼主| 发表于 2010-12-21 13:06:56 | 显示全部楼层
回复 5# jeongee


   回头测试一下
发表于 2010-12-22 12:21:34 | 显示全部楼层
回复 7# puras


    呵呵,你不用测试了,如果 MySQL 这个都做不好的话,那他还能这么流行吗?
这是直接调用的 MySQL 接口,不是 CI 自己做的。
发表于 2010-12-24 16:36:51 | 显示全部楼层
在插入数据后,马上执行 $this->db->insert_id()
发表于 2010-12-25 23:14:32 | 显示全部楼层
凑热闹来了,楼上说的对!

本版积分规则