[新手请问]PHP写入MySQL数据库的有关问题

php中文网
发布: 2016-06-13 13:31:09
原创
1235人浏览过

[新手请教]PHP写入MySQL数据库的问题
现在利用PHP 数组生成了从1-33的任选6个数字的组合数据,约100万条的数据生成了数据.TXT文件,格式如下
[1,2,3,4,5,6]
[1,2,3,4,5,7]
[1,2,3,4,5,8]
[1,2,3,4,5,9]
[1,2,3,4,5,10]
[1,2,3,4,5,11]
[1,2,3,4,5,12]
[1,2,3,4,5,13]
[1,2,3,4,5,14]
[1,2,3,4,5,15]
[1,2,3,4,5,16]
[1,2,3,4,5,17]
[1,2,3,4,5,18]
...............
现在希望将这样的数据入库.现在有数据库表HMK,结构为
"hmkid","int(11)","NO","PRI","","auto_increment"
"l1","int(10) unsigned","NO","","",""
"l2","int(10) unsigned","NO","","",""
"l3","int(10) unsigned","NO","","",""
"l4","int(10) unsigned","NO","","",""
"l5","int(10) unsigned","NO","","",""
"l6","int(10) unsigned","NO","","",""
请问如何在程序运行过程中编写PHP程序直接将数据写入数据库而不存入txt文档啊?
我的想法

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$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."</br>" ;
     $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();
登录后复制

问题2:要导入TXT文档到数据库的程序有又要怎么写啊?我希望L1存储第一个数据L2存储第二个数据依次类推,

多谢帮忙.生成1-33的组合数据程序可以参考
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$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;
}


登录后复制


------解决方案--------------------
PHP code
$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-&gt;query($sql);
if( ! $result ){
    echo $db-&gt;error();
    var_dump($sql);
}
<br><font color="#e78608">------解决方案--------------------</font><br>每次查询只插入一条记录效率太低了,可以批量插入,比如每次50000条:<br><br>
登录后复制
PHP code
$data = array(array(1,2,3,4,5,6), array(1,2,3,4,57)); //先生成这样的data数组,每个元素是一组号码的数组
$idx = 0;
while($idx query($sql);
    $idx += 50000;
}
$db-&gt;close();
<br><font color="#e78608">------解决方案-------------------- <div class="clear"></div></font>
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号