# 创建mysql数据表:
CREATE TABLE `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(20) NOT NULL,
`content` varchar(50) NOT NULL,
`count` int(11) NOT NULL,
`dtime` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
# PHP file name:index.php
$con = mysql_pconnect("127.0.0.1","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("demo", $con);
for($n=0;$n<=100;$n++){//每次请求批量插入200000笔数据
$sql='';
for($i=0;$i<=2000;$i++){ //一次批量插入2000笔数据
$title=rand_str(19);//随即产生数据
$content=rand_str(40);
$count=rand(1,80);
$dtime=time();
//echo $title.'-'.$content.'-'.$count.'-'.$dtime.'<br/>';
$sql.=",('".$title."','".$content."',$count,'".$dtime."')";
}
$sql="insert into news(title,content,count,dtime) values('".$title."','".$content."',$count,'".$dtime."')".$sql;
mysql_query($sql,$con);
}
mysql_close($con);
header("Location:http://127.0.0.1/index.php"); //为了防止php超时跳转刷新页面
function rand_str($len){
$return_str='';
$rand_string="abcdefghigklmnoqprstuvwxyz0123456789";
for($i=1;$i<=$len;$i++)
$return_str.=substr($rand_string,rand(0,35),1);
return $return_str;
}
转载于:https://my.oschina.net/u/1270458/blog/163898