本文目錄一覽:
- 1、php如何將存有數據的文件導入資料庫
- 2、php怎麼讀取txt文本內容存入mysql資料庫
- 3、php 接收到之後post數據寫入資料庫
- 4、php如何寫入資料庫
- 5、php獲取文本內容導入資料庫
- 6、如何利用php讀取txt文件再將數據插入到資料庫
php如何將存有數據的文件導入資料庫
其實sql文件,就是一些sql語句
填寫好資料庫相關操作後,點擊下一步,首先將資料庫連接起來
12mysql_connect(…………..)//等等這些資料庫連接代碼
資料庫連接後,開始讀取sql文件
1234567$Sqls = file_get_contents( ‘你的sql文件’ );//然後把讀取到的sql文件內容打散成數組,當然,這個文件要有規律,就是每條sql語句有一個特定的分隔符,比如分號;$SqlArr = explode(‘;’, $Sqls );//最後就是循環遍歷出這些sql語句並執行,即可foreach ( $SqlArr as $sql ) { mysql_query( $Sql );}
上面只是一個大致思路原理,
具體的話,還是要根據具體情況來弄的!
特別是那個sql文件中的內容,一定要有一定的規律,並且一些不必要的東西不能有,
比如注釋(很多人從phpmyadmin導出的sql文件,都會帶上注釋,
而注釋是不符合sql語句規範的,會執行出錯,
所以導出後,自己根據情況修改一下!)
php怎麼讀取txt文本內容存入mysql資料庫
第一步,讀取txt的文件。假設為a.txt
$content = file_get_content(‘a.txt’); //讀取文件內容存入變數。
第二步,存入資料庫
mysql_query(“insert 表名 (欄位名) values(‘”.$content.”‘));
Ps:文件是上傳的,上傳後的臨時文件名是:$_FILE[‘tmp_name’]
php 接收到之後post數據寫入資料庫
form表單demo:task.html
fieldset id=”setFiled”
legend發布任務/legend
form action=”registr.php” method=”post” id=”steForm”
label任務類型:/labelbr
input type=”text” name=”type” id=”taskType” placeholder=”請選擇任務類型”/br
label酬nbsp;nbsp;金:/labelbr
input type=”number” name=”money” id=”forMoney” min=”1″ max=”1000″/label元/labelbr
label截止時間:/labelbr
input type=”datetime” name=”time” id=”timeSubmit”/span data-year=”” data-month=”” data-date=”” id=”showDate”/spanbr
label詳細描述:/labelbr
textarea maxlength=”512″ name=”textAray” id=”msgArea”/textareabr
input type=”submit” name=”subMit” id=”forSub” value=”點擊發布” /
/form
擴展資料
php接收POST數據的三種方式
1、$_POST 方式接受數據
$_POST 方式是由通過HTTP的POST方法傳遞過來的數據組成的數組,是一個自動全局變數。
註:只能接收Content-Type:application/x-www-form-urlencode提交的數據。也就是只能接收表單過來的數據。
2、GLOBLES[『HTTP_RAW_POST_DATA』]
如果訪問原始POST數據不是php能夠識別的文檔類型,比如:text/xml 或者soap等等,可以用$GLOBLES[『HTTP_RAW_POST_DATA』]來接收,$HTTP_RAW_POST_DATA變數包含有原始POST數據。此變數僅在碰到未識別的MIME數據時產生。
註:$HTTP_RAW_POST_DATA對於enctype=」multipart/form-data」表單數據不可用,也就是說使用$HTTP_RAW_POST_DATA無法接受網頁表單post過來的數據。
3、file_get_contents(「php://input」);
如果訪問原始POST數據,更好的方法是使用file_get_content(「php://input」);對於未指定Content-Type的POST數據,可以使用該方法讀取POST原始數據,包括二進位流也可以和$HTTP_RAW_POST_DATA比起來。它帶來的生存眼裡更小,並且不需要任何特殊的php.ini設置。
註:php://input不能用於 enctype=」multipart/form-data」
例如:$postStr = file_get_contents(“php://input”); //獲取POST數據
php如何寫入資料庫
數組吧,直接把數組轉字元串啊
implode() 函數返回由數組元素組合成的字元串。(適合一維數組)
$arr = array(‘Hello’, ‘World’, ‘I’, ‘love’, ‘Shanghai’);
1 echo implode(” “,$arr);//加空格
the result : Hello World I love Shanghai
2 echo implode(“,”,$arr);//加逗號
the result : Hello,World,I,love,Shanghai
轉換數組為字元串後插入資料庫就可以了。
php獲取文本內容導入資料庫
看你說的title和time這種格式應該是XML文檔,如果XML文檔,完全不用自己讀取文檔,可以使用PHP提供的simpleXML庫區操作,然後再用PDO或MySQLi庫去寫入資料庫即可。
如何利用php讀取txt文件再將數據插入到資料庫
serial_number.txt的示例內容:
serial_number.txt:
DM00001A11 0116,
SN00002A11 0116,
AB00003A11 0116,
PV00004A11 0116,
OC00005A11 0116,
IX00006A11 0116,
創建數據表:
create table serial_number(
id int primary key auto_increment not null,
serial_number varchar(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
php代碼如下:
$conn = mysql_connect(‘127.0.0.1′,’root’,”) or die(“Invalid query: ” . mysql_error());
mysql_select_db(‘test’, $conn) or die(“Invalid query: ” . mysql_error());
$content = file_get_contents(“serial_number.txt”);
$contents= explode(“,”,$content);//explode()函數以”,”為標識符進行拆分
foreach ($contents as $k = $v)//遍歷循環
{
$id = $k;
$serial_number = $v;
mysql_query(“insert into serial_number (`id`,`serial_number`)
VALUES(‘$id’,’$serial_number’)”);
}
備註:方法有很多種,我這裡是在拆分txt文件為數組後,然後遍歷循環得到的數組,每循環一次,往資料庫中插入一次。
再給大家分享一個支持大文件導入的
?php
/**
* $splitChar 欄位分隔符
* $file 數據文件文件名
* $table 資料庫表名
* $conn 資料庫連接
* $fields 數據對應的列名
* $insertType 插入操作類型,包括INSERT,REPLACE
*/
function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType=’INSERT’){
if(empty($fields)) $head = “{$insertType} INTO `{$table}` VALUES(‘”;
else $head = “{$insertType} INTO `{$table}`(`”.implode(‘`,`’,$fields).”`) VALUES(‘”; //數據頭
$end = “‘)”;
$sqldata = trim(file_get_contents($file));
if(preg_replace(‘/\s*/i’,”,$splitChar) == ”) {
$splitChar = ‘/(\w+)(\s+)/i’;
$replace = “$1′,'”;
$specialFunc = ‘preg_replace’;
}else {
$splitChar = $splitChar;
$replace = “‘,'”;
$specialFunc = ‘str_replace’;
}
//處理數據體,二者順序不可換,否則空格或Tab分隔符時出錯
$sqldata = preg_replace(‘/(\s*)(\n+)(\s*)/i’,’\’),(\”,$sqldata); //替換換行
$sqldata = $specialFunc($splitChar,$replace,$sqldata); //替換分隔符
$query = $head.$sqldata.$end; //數據拼接
if(mysql_query($query,$conn)) return array(true);
else {
return array(false,mysql_error($conn),mysql_errno($conn));
}
}
//調用示例1
require ‘db.php’;
$splitChar = ‘|’; //豎線
$file = ‘sqldata1.txt’;
$fields = array(‘id’,’parentid’,’name’);
$table = ‘cengji’;
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo ‘Success!br/’;
}else {
echo ‘Failed!–Error:’.array_shift($result).’br/’;
}
/*sqlda ta1.txt
1|0|A
2|1|B
3|1|C
4|2|D
— cengji
CREATE TABLE `cengji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
*/
//調用示例2
require ‘db.php’;
$splitChar = ‘ ‘; //空格
$file = ‘sqldata2.txt’;
$fields = array(‘id’,’make’,’model’,’year’);
$table = ‘cars’;
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo ‘Success!br/’;
}else {
echo ‘Failed!–Error:’.array_shift($result).’br/’;
}
/* sqldata2.txt
11 Aston DB19 2009
12 Aston DB29 2009
13 Aston DB39 2009
— cars
CREATE TABLE `cars` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`make` varchar(16) NOT NULL,
`model` varchar(16) DEFAULT NULL,
`year` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
*/
//調用示例3
require ‘db.php’;
$splitChar = ‘ ‘; //Tab
$file = ‘sqldata3.txt’;
$fields = array(‘id’,’make’,’model’,’year’);
$table = ‘cars’;
$insertType = ‘REPLACE’;
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);
if (array_shift($result)){
echo ‘Success!br/’;
}else {
echo ‘Failed!–Error:’.array_shift($result).’br/’;
}
/* sqldata3.txt
11 Aston DB19 2009
12 Aston DB29 2009
13 Aston DB39 2009
*/
//調用示例3
require ‘db.php’;
$splitChar = ‘ ‘; //Tab
$file = ‘sqldata3.txt’;
$fields = array(‘id’,’value’);
$table = ‘notExist’; //不存在表
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo ‘Success!br/’;
}else {
echo ‘Failed!–Error:’.array_shift($result).’br/’;
}
//附:db.php
/* //注釋這一行可全部釋放
?
?php
static $connect = null;
static $table = ‘jilian’;
if(!isset($connect)) {
$connect = mysql_connect(“localhost”,”root”,””);
if(!$connect) {
$connect = mysql_connect(“localhost”,”Zjmainstay”,””);
}
if(!$connect) {
die(‘Can not connect to database.Fatal error handle by /test/db.php’);
}
mysql_select_db(“test”,$connect);
mysql_query(“SET NAMES utf8”,$connect);
$conn = $connect;
$db = $connect;
}
?
//*/
.
— 數據表結構:
— 100000_insert,1000000_insert
CREATE TABLE `100000_insert` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
100000 (10萬)行插入:Insert 100000_line_data use 2.5534288883209 seconds
1000000(100萬)行插入:Insert 1000000_line_data use 19.677318811417 seconds
//可能報錯:MySQL server has gone away
//解決:修改my.ini/my.cnf max_allowed_packet=20M
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242518.html