我有我與工作表中的一個怪異的行爲,我不知道這是否是我的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