# 建立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; }