使用循环向数据库中插入数据:
createBigData.php
<?php
/*
- 向数据库中插入数据,数万级别 / set_time_limit(0); require_once 'connDB.php'; $table = 'user'; //数据库表 $conn = ConnDB::singleton(); //调用单例模式进行数据库操作 //$conns = ConnDB::singleton(); //调用单例模式进行数据库操作 addData($table, $conn); /
- 批量向数据库中插入数据
*/
function addData($table, $conn) {
for($i=1; $i<=100000; $i++){
$name = getName();
$sex = rand(0, 3);
$age = rand(1, 120);
$insert = "insert into ".$table." values( null, $i, '$name', '$sex', '$age')";
$conn->query($insert);
//echo "
insert:".$insert; }//end for() }//end func addData()
//随机获取姓名字符串
function getName() {
$name = '';
$singleWord = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$singleWords = 'abcdefghijklmnopqrstuvwxyz';
$length = strlen($singleWord);
for ($i = 0; $i < rand(3, $length); $i++) {
if ($i == 0) {
$name .= substr($singleWord, rand(1, $length)-1, 1);
}else {
$name .= substr($singleWords, rand(1, $length)-1, 1);
}//end if()
}//end for
return $name;
}//end func getName
?>
插入数据之后的查询结果:
与第一条name重复的记录:
本来要的十万条数据都有了,但是通过查询发现这样一个规律:name字段每隔1760条记录就会重复,而且不止name重复,sex、age也会一模一样,不知道是怎么回事,求各位大神解惑!