【发布时间】:2014-12-09 13:47:16
【问题描述】:
我遇到了一些问题,从 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
【问题讨论】: