透過循環(huán)的方式為什麼插入到資料庫中的id列始終從1開始?
<?php
$pdo = new PDO('mysql://host=localhost;dbname=test', 'root', 'root');
$pdo->exec('use test');
$pdo->exec('set names utf8');
$str = str_repeat('hi', 500);
$arr = range(1, 20);//生成1-20的數(shù)字,作為id列的值
shuffle($arr);//打亂id值在數(shù)組中的順序
$statem= $pdo->prepare("insert into t2(id,c2,c3) values(?,?,?)");
foreach ($arr as $value)
{
//不明白這里為什么插入到數(shù)據(jù)庫中的id列的值始終從1開始,而打印出來的$value并不是從1開始的,而是亂序的
$statem->execute(array($value,$str,$str));
}
//下面代碼插入id值到數(shù)據(jù)庫時數(shù)據(jù)庫的id列是無序的,我要的就是這里的效果,但想通過12~16行的循環(huán)方式實現(xiàn)
$a = 22;
$b = 25;
$c = 44;
$r = $pdo->exec("insert into t2(id,c2,c3) values ($a,'$str','$str')");
$r = $pdo->exec("insert into t2(id,c2,c3) values ($b,'$str','$str')");
$r = $pdo->exec("insert into t2(id,c2,c3) values ($c,'$str','$str')");
下面是t2表的結(jié)構(gòu)
擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項目經(jīng)理、高級軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...