[新手请问]PHP写入MySQL数据库的有关问题
[新手请教]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."" ; $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
------解决方案--------------------
- 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->query($sql); if( ! $result ){ echo $db->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->close(); <br><font color="#e78608">------解决方案-------------------- <div class="clear"> </div></font>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

ReactQuery database plug-in: Methods to implement data import and export, specific code examples are required. With the widespread application of ReactQuery in front-end development, more and more developers are beginning to use it to manage data. In actual development, we often need to export data to local files or import data from local files into the database. In order to implement these functions more conveniently, you can use the ReactQuery database plug-in. The ReactQuery database plugin provides a series of methods

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

This article will introduce how to use PowerQuery to split data into rows. When exporting data from other systems or sources, it is common to encounter situations where the data is stored in cells combining multiple values. With PowerQuery, we can easily split such data into rows, making the data easier to process and analyze. This can happen if the user doesn't understand Excel's rules and accidentally enters multiple data into a cell, or if the data is not formatted correctly when copying/pasting it from other sources. Processing this data requires additional steps to extract and organize the information for analysis or reporting. How to split data in PowerQuery? PowerQuery transformations can be based on a variety of different factors such as word

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is
