时间:2021-07-01 10:21:17 帮助过:5人阅读
$db=new mysqli("localhost","admin","admin","ssq"); $db->query("truncate table hmk"); $info = array(1, 2, 3,4,5,6);//$info是中间生成的的组合数组. foreach ($info as $skey=>$value){ //echo 'my array is underline:'.$key."=>".$value."" ; $query="insert into hmk(l1,l2,l3,l4,l5,l6) values($values)"; $result=$db->query($query) ; } if (result) echo "good luck" ; else echo "sorry" ; $db->close();
$a = range(1, 33); $ar = combination($a, 6); //求组合高效率的10移动法 function combination($numArr,$combineLen) { $numCt = count($numArr); if($combineLen > $numCt) return; $bin = str_pad('',$combineLen,'1'); $bin = str_pad($bin,$numCt,'0',STR_PAD_RIGHT); $find = $bin; $rs[] = implode(' ',array_slice($numArr,0,$combineLen)); $j = 1; while(strrev($find) != $bin) { $k = explode('10',$find,2); $find = $find{0} === '0' ? strrev($k[0]).'01'.$k[1] : $k[0].'01'.$k[1]; for($i=0;$i<$numCt;$i++) $rs[$j] .= $find[$i] ? $numArr[$i] . " " : ''; $j++; } return $rs; }
$fp = fopen('数据.txt', 'r'); while( !feof($fp) ){ $line = trim( fget($fp) ); $sqlvalue .= '('. substr($line, 1, -1) . '),'; } $sql = "insert into hmk(l1,l2,l3,l4,l5,l6) values "; $sql = $sql . substr($sqlvalue, -1, 1); // 去掉末尾的 , $db = new mysqli("localhost","admin","admin","ssq"); $result = $db->query($sql); if( ! $result ){ echo $db->error(); var_dump($sql); }
------解决方案--------------------
每次查询只插入一条记录效率太低了,可以批量插入,比如每次50000条:
$data = array(array(1,2,3,4,5,6), array(1,2,3,4,57)); //先生成这样的data数组,每个元素是一组号码的数组 $idx = 0; while($idx < count($data) - 1)) { $ar = array(); if ($idx + 50000 < count($data)) $ar = array_slice($data, $idx, 50000); else $ar = array_slice($data, $idx); $sql = 'INSERT INTO htm(l1,l2,l3,l4,l5,l6) VALUES '; $ar_values = array(); foreach($ar as $v) $ar_values[] = '('.join(',', $v).')'; $sql .= join(',', $ar_values); $result = $db->query($sql); $idx += 50000; } $db->close();
------解决方案--------------------