【问题标题】:Codeigniter $this->db->insert_id() returning 0 on mysqli but $this->db->query('SELECT LAST_INSERT_ID()') worksCodeigniter $this->db->insert_id() 在 mysqli 上返回 0 但 $this->db->query('SELECT LAST_INSERT_ID()') 有效
【发布时间】:2016-12-29 19:49:37
【问题描述】:

这是我的数据库配置:-

$db['default'] = array(
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'test_db',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );

我使用这个插入用户并在die();上输出最后一个ID

$this->db->insert("user",$data);
$lastid=$this->db->insert_id();
die($lastid);`

它输出了0

但是当我使用时:-

$query = $this->db->query('SELECT LAST_INSERT_ID()');
$row = $query->row_array();
$lastid= $row['LAST_INSERT_ID()'];
die($lastid);

它输出了最后插入的id...

我在user_id 上有一个auto_increment

为什么$this->db->insert_id() 不起作用?

【问题讨论】:

标签: php mysql codeigniter mysqli


【解决方案1】:

我认为没有返回 id,因为您的自动增量值不仅仅是普通 ID,而是 user_id.. 这是因为我尝试过

$this->db->insert("user",$data);
$lastid=$this->db->insert_id();
die($lastid);

使用具有 ID 的表并且它可以工作,但是当我在同一个项目中尝试使用带有 item_id 的表时它不起作用

【讨论】:

    【解决方案2】:

    $this->db->insert_id() 返回进行插入的最后一行的 id,确保您已将 id 列设置为主键以自动递增。用这个;

    $this->db->insert('table',$array(''=>,));
    $id=$this->db->insert_id();`enter code here`
    return $id;
    

    【讨论】:

      【解决方案3】:

      根据我给你的链接(在 cmets 中):-

      1.从第一个链接($this->db->insert_id(); returning 0 every time in codeigniter) 你可能没有主键自增列。所以你可以使用:-

      $this->db->affected_rows()
      

      (但这不会给你最后插入的id,它会告诉你有多少行因为插入查询而受到影响)

      2.从第二个链接(https://kedyr.wordpress.com/2012/10/03/codeigniter-insert_id/)给出了完全相同的解决方案,您在第二次尝试中尝试过(这对您有用),但没有解释为什么会发生这种行为。

      3.基于:- http://forum.codeigniter.com/thread-61881.html

      MySQLi 在提交事务时重置插入 ID(预期行为)。当它没有事务时,我认为由于错误,PHP 版本之间的行为会有所不同。尝试将其包装在事务中,通过调用 trans_begin()、insert()、insert_id()、commit()。

      但我认为这是一个非常高端的概念(即使我不知道如何执行此操作,所以没有 cmets)。

      4.来自此链接:- https://github.com/bcit-ci/CodeIgniter/issues/591 它再次说:-

      a.如果没有 auto_increment 列,它将返回 0

      b.没有 mysqli 连接/链接 id 会返回 false(在 php 中实际上是 0)。

      最后一次尝试一下:-

      $this->db->insert("user",$data);
      $lastid=$this->db->insert_id();
      echo $lastid;die;
      

      【讨论】:

      • 虽然我有自动增量,但它仍然无法正常工作。 $this->db->insert_id() 以前工作过,但我想我会使用第二个选项 $query = $this->db->query('SELECT LAST_INSERT_ID()'); $row = $query->row_array(); $lastid= $row['LAST_INSERT_ID()'];死($lastid);仍然想知道为什么 $this->db->insert_id() 不起作用。
      • $this->db->affected_rows() 或者我建议的最后一个呢?
      • @Vlanzyvinz 我也想知道(不是因为它不起作用,因为没有对此给出解释)
      • 我试试 $this->db->affected_rows() 我刚试过 db->trans_start() db->trans_complete(); $this->db->trans_start();<br> $this->db->insert("uhd_voucher_menu",$data);<br> $lastid=$this->db->insert_id();<br> $this->db->trans_complete();<br> die($lastid); 还是没有得到id
      • @Vlanzyvinz 最后一个呢。 echo $lastid;die; ?
      【解决方案4】:

      我已经检查过,它工作正常。请这样尝试

      public function addNewAdmin($data) {
      
              $result = $this->db->insert("admins", $data);
              $lastid=$this->db->insert_id();        
              echo $lastid;exit;        
      }
      

      【讨论】:

      • 是否有必要将这些东西包装在一个函数中(我不是批评只是好奇,因为我对 CI 的了解非常少)
      • 该函数应该在模型类中。因为 CI 是 MVC。然后在控制器上我们可以通过 $this->Model_name->addNewAdmin($data); 调用模型
      猜你喜欢
      • 2015-12-15
      • 2017-04-10
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      相关资源
      最近更新 更多