mysqli自动增量跳跃数字

时间:2014-12-09 13:47:17

标签: php mysqli

我遇到一些问题,其中从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 个答案:

答案 0 :(得分:0)

认为我已经解决了我自己的问题,我插入的一个标题有一个单引号',它正在逃避导致问题的插入,不知道为什么它会返回一个id作为响应虽然