欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php文本文件加入數(shù)據(jù),php讀取文本文件

如何利用php讀取txt文件再將數(shù)據(jù)插入到數(shù)據(jù)庫

serial_number.txt的示例內(nèi)容:

10年積累的成都網(wǎng)站制作、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有平武免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

serial_number.txt:

DM00001A11 0116,

SN00002A11 0116,

AB00003A11 0116,

PV00004A11 0116,

OC00005A11 0116,

IX00006A11 0116,

創(chuàng)建數(shù)據(jù)表:

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()函數(shù)以","為標(biāo)識符進(jìn)行拆分

foreach ($contents as $k = $v)//遍歷循環(huán)

{

$id = $k;

$serial_number = $v;

mysql_query("insert into serial_number (`id`,`serial_number`)

VALUES('$id','$serial_number')");

}

備注:方法有很多種,我這里是在拆分txt文件為數(shù)組后,然后遍歷循環(huán)得到的數(shù)組,每循環(huán)一次,往數(shù)據(jù)庫中插入一次。

再給大家分享一個支持大文件導(dǎo)入的

?php

/**

* $splitChar 字段分隔符

* $file 數(shù)據(jù)文件文件名

* $table 數(shù)據(jù)庫表名

* $conn 數(shù)據(jù)庫連接

* $fields 數(shù)據(jù)對應(yīng)的列名

* $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('"; //數(shù)據(jù)頭

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

}

//處理數(shù)據(jù)體,二者順序不可換,否則空格或Tab分隔符時出錯

$sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替換換行

$sqldata = $specialFunc($splitChar,$replace,$sqldata); //替換分隔符

$query = $head.$sqldata.$end; //數(shù)據(jù)拼接

if(mysql_query($query,$conn)) return array(true);

else {

return array(false,mysql_error($conn),mysql_errno($conn));

}

}

//調(diào)用示例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

*/

//調(diào)用示例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

*/

//調(diào)用示例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

*/

//調(diào)用示例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;

}

?

//*/

.

-- 數(shù)據(jù)表結(jié)構(gòu):

-- 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

php獲取文本內(nèi)容導(dǎo)入數(shù)據(jù)庫

看你說的title和time這種格式應(yīng)該是XML文檔,如果XML文檔,完全不用自己讀取文檔,可以使用PHP提供的simpleXML庫區(qū)操作,然后再用PDO或MySQLi庫去寫入數(shù)據(jù)庫即可。

PHP將數(shù)據(jù)寫入txt文件

//記錄返回值

? ? $write_data_a = [

? ? ? ? 'html_url'? =? $getUrl,

? ? ? ? 'ip'? ? = $this-get_real_ip(),

? ? ? ? 'time'? =? date("Y-m-d H:i:s",time()),

? ? ? ? 'res'?? = $response

? ? ];

//轉(zhuǎn)化為JSON

? ? $write_data_a = json_encode($write_data_a) . '||' . "\n";

? ? $date = date("Y-m-d", time());

//項(xiàng)目路徑目錄,判斷是否存在,不存在則創(chuàng)建

? ? $lujing = "./360_mobile_res_sd";

? ? if(!is_dir($lujing)){

? ? ? ? mkdir(iconv("UTF-8", "GBK", $lujing),0777,true);

? ? }

//文件,判斷是否存在,不存在則創(chuàng)建

? ? $TxtFileName = "./360_mobile_res_sd/" . $date . "_2.txt";

? ? //以讀寫方式打?qū)懼付ㄎ募绻募淮鎰t創(chuàng)建

? ? if(file_exists($TxtFileName))

? ? {

//存在,追加寫入內(nèi)容

? ? ? ? file_put_contents($TxtFileName, $write_data_a, FILE_APPEND);

? ? }

? ? else

? ? {

//不存在,創(chuàng)建并寫入

? ? ? ? if( ($TxtRes=fopen ($TxtFileName,"w+")) === FALSE){

? ? ? ? ? ? exit();

? ? ? ? }

? ? ? ? if(!fwrite ($TxtRes,$write_data_a)){ //將信息寫入文件

? ? ? ? ? ? fclose($TxtRes);

? ? ? ? ? ? exit();

? ? ? ? }

? ? ? ? fclose ($TxtRes); //關(guān)閉指針

? ? }

網(wǎng)頁標(biāo)題:php文本文件加入數(shù)據(jù),php讀取文本文件
文章出自:http://chinadenli.net/article23/dsipdjs.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站標(biāo)簽優(yōu)化網(wǎng)站排名動態(tài)網(wǎng)站外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)