当前位置: 首页 > 知识库问答 >
问题:

mysqli自动递增跳转数

孔鸿宝
2023-03-14

我遇到了一些问题,从mysql返回的id不正确。我的代码创建了一个集合,然后将从数据库返回的id传递给另一个函数,该函数将项目添加到数据库中,并将集合的id作为参数传递,以建立关系。

  // Create new collection
  private function createCollection( $item )
  {    
    $query = "INSERT INTO collection VALUES (
        '',
        ' " . $item['title_en'] ."',
        ' " . $item['title_cy'] ."',
        ' " . $item['description_en'] ."',
        ' " . $item['description_cy'] ."',
        ' " . $tagString . " ',
        ' " . $item['cover'] . " ',
        ' " . $item['guid'] . " ',
        ' " . $item['website'] . " ',
        ' " . $item['date'] . " ',
        '',
        ''
    )";

    $this->mysqli->query($query);    

    $collectionId = $this->mysqli->insert_id;

    echo "\nAdded item with id $collectionId\n";

    $this->addItemToDatabase( $item , $collectionId );
  }

  // Create item
  private function addItemToDatabase( $item , $collectionId )
  {
    foreach($item['items'] as $i) {
      $query = "INSERT INTO items VALUES (
          '',
          ' " . $i['title'] ."',
          ' " . $i['description'] ."',
          ' " . $i['creator'] . " ',
          ' " . $i['owner'] . " ',
          ' " . $i['image'] . " ',
          ' " . $i['date'] . " ',
          ' " . $collectionId . " ',
          '',
          ''
      )";

      $this->mysqli->query($query);     
    }
  }

问题是一段时间后,从数据库返回的ID不正确

Added item with id 47
Added item with id 48
Added item with id 49
Added item with id 50
Added item with id 51
Added item with id 664
Added item with id 52
Added item with id 670
Added item with id 676
Added item with id 53
Added item with id 54
Added item with id 55
Added item with id 736
Added item with id 56
Added item with id 57
Added item with id 767
Added item with id 58
Added item with id 59
Added item with id 796
Added item with id 60
Added item with id 61
Added item with id 834
Added item with id 62
Added item with id 846

数据库中没有id为数百的集合。我不太确定为什么mysqli返回不存在的错误数字。

mysql --version
mysql  Ver 14.14 Distrib 5.5.40, for Linux (x86_64) using readline 5.1

共有1个答案

燕烨
2023-03-14

我想我已经解决了我自己的问题,我插入的其中一个标题有一个单引号,它正在逃避导致问题的插入,但不确定为什么它会返回一个id作为响应

 类似资料:
  • 问题内容: 我的架构看起来像这样: 我已经在同一数据库中创建了counters集合,并添加了一个带有’entityId’的_id的页面。从这里我不确定如何使用猫鼬来更新该页面并获取递增编号。 没有计数器的架构,我希望它保持这种状态,因为这实际上不是应用程序使用的实体。仅应在模式中使用它来自动递增字段。 问题答案: 这是一个示例,如何在Mongoose中实现自动增量字段:

  • 问题内容: 我有一个使用hibernate和注解的j2ee应用程序。我如何注释我的pojo类中的Id字段以将其设置为自动增量或自动生成。在添加bean时,我是否将该字段留在bean中为null? 问题答案: 并在保留时将其保留为。(如果使用包装器) 在某些情况下,该策略被解析为或,因此你可能需要手动将其设置为(取决于基础数据库)。 似乎+ 指定序列名称对你有用。

  • 问题内容: 使用MySQL(特别是5.0)有没有办法让auto_increment字段的值基于分组列? 例: 我不想通过任何“疯狂”的方法来达到这个目的,但是如果需要的话。 问题答案: 对于MyISAM和BDB表,您可以将auto_increment字段作为键的次要部分,例如 这是手册对此所说的 在这种情况下,将为AUTO_INCREMENT列生成的值计算为MAX(auto_increment_c

  • 问题内容: 我现在正在一家公司中使用全球DBA的无限智慧,他创建了一个表,该表将一个int用作ID字段,但是不会自动递增该数字。 我要从.Net传递一个表值参数,因为它随时有大约100或更多行的数据正在传递,并且我不想杀死该应用程序,破坏网络或SQL Server。 这是我的存储过程 我希望这会为我带来一些帮助,就像我对此进行测试时一样 这就是我回来的 (影响1行) (影响1行) (影响1行) (

  • 我在下面的代码中创建了一个表: 此外,我有记录从csv文件导入到表。我想插入一个新的记录到表我一直得到这个错误 错误:重复的键值违反了唯一约束"salesorderdetail_pkey"DETAIL: Key(SalesorderDetid)=(2)已经存在。********** 错误 ********** 错误:重复的键值违反唯一约束“salesorderdetail_pkey”SQL状态:2

  • 问题内容: 我有一个使用hibernate和注解的j2ee应用程序。我如何注释我的pojo类中的Id字段以将其设置为自动增量或自动生成。在添加bean时,我是否将该字段留在bean中为null? 问题答案: 并在保留时将其保留为()。(如果使用/ 包装器) 在某些情况下,该策略被解析为to 或to ,因此您可能需要手动将其设置为or (取决于基础数据库)。 似乎 + 指定序列名称 对您有用。