我有我与工作表中的一个怪异的行为,我不知道这是否是我的PHP代码或数据库上,是造成自动递增的ID掉下来的一些设置同步。不同步自动递增MYSQL
如果我的名字运行没有任何索引下面的代码&车,我得到:
$cars = array("Volvo","BMW","Toyota");
$name = "John Smith";
foreach($cars as $value)
{
try
{
//insert into database with a prepared statement
$query = $db->prepare(
'INSERT INTO cars (name,cars)
VALUES (:name,:cars)
');
$query->execute(array(
':name' => $name,
':cars' => $value
));
}
//else catch the exception and show the error.
catch(PDOException $e)
{
$error[] = $e->getMessage();
}
}
///Results
id || name || cars
1 || John Smith || Volvo
2 || John Smith || BMW
3 || John Smith || Toyota
但是,如果我把一个唯一索引名&汽车,自动递增变得不同步,我可以”不明白为什么,因为我看不到我的PHP代码有什么问题?
$cars = array("Volvo","BMW","Toyota");
$name = "John Smith";
foreach($cars as $value)
{
try
{
//insert into database with a prepared statement
$query = $db->prepare(
'INSERT INTO cars (name,cars)
VALUES (:name,:cars)
');
$query->execute(array(
':name' => $name,
':cars' => $value
));
}
//else catch the exception and show the error.
catch(PDOException $e)
{
$error[] = $e->getMessage();
}
}
///Results
id || name || cars
3 || John Smith || Toyota
1 || John Smith || Volvo
2 || John Smith || BMW
除非您的SELECT语句具有ORDER BY子句,否则__no不保证您的记录将以任何特定顺序返回 – 2014-10-31 14:22:33
为什么要将名称/汽车作为唯一索引? – 2014-10-31 14:22:58
我只想要一个名字和汽车的组合。除此之外,这对我来说没有用,因为我只会获得相同的信息,以至于这个人拥有这辆名车,其他所有东西都会作为数据库中的重复部分。 – user3882771 2014-10-31 14:36:10