php代碼咋導(dǎo)入數(shù)據(jù)庫(kù) php導(dǎo)入php文件

如何用thinkphp實(shí)現(xiàn)將excel數(shù)據(jù)導(dǎo)入到mysql中

第一步:建立數(shù)據(jù)庫(kù)和數(shù)據(jù)表(按照自己的Excel數(shù)據(jù)設(shè)立字段)。

創(chuàng)新互聯(lián)是專業(yè)的凌源網(wǎng)站建設(shè)公司,凌源接單;提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行凌源網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

[sql] view plain copy print?

CREATE DATABASE php_excel;

USE php_excel;

CREATE TABLE IF NOT EXISTS php_excel(

id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,

gid varchar(20) NOT NULL,

stu_no varchar(20) NOT NULL,

name varchar(45) NOT NULL,

age int(4) NOT NULL

)ENGINE=MyISAM DEFAULT CHARSET=utf8;

第二步:前臺(tái)index.php文件。

[html] view plain copy print?

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""

html xmlns=""

head

meta http-equiv="Content-Type" content="text/html; charset=utf-8" /

titlephpexcel導(dǎo)入excel數(shù)據(jù)到MYSQL數(shù)據(jù)庫(kù)/title

/head

body

form name="frm1" action="insertdb.php" method="post" enctype="multipart/form-data"

input name="filename" type="file" /

input name="submit" type="submit" value="import" /

/form

/body

/html

第三步:向數(shù)據(jù)庫(kù)插入數(shù)據(jù)的insertdb.php文件。

[php] view plain copy print?

session_start();

header("Content-type:text/html;charset:utf-8");

//全局變量

$succ_result=0;

$error_result=0;

$file=$_FILES['filename'];

$max_size="2000000"; //最大文件限制(單位:byte)

$fname=$file['name'];

$ftype=strtolower(substr(strrchr($fname,'.'),1));

//文件格式

$uploadfile=$file['tmp_name'];

if($_SERVER['REQUEST_METHOD']=='POST'){

if(is_uploaded_file($uploadfile)){

if($file['size']$max_size){

echo "Import file is too large";

exit;

}

if($ftype!='xls'){

echo "Import file type is error";

exit;

}

}else{

echo "The file is not empty!";

exit;

}

}

require("./conn.php"); //連接mysql數(shù)據(jù)庫(kù)

//調(diào)用phpexcel類庫(kù)

require_once 'PHPExcel.php';

require_once 'PHPExcel\IOFactory.php';

require_once 'PHPExcel\Reader\Excel5.php';

$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format

$objPHPExcel = $objReader-load($uploadfile);

$sheet = $objPHPExcel-getSheet(0);

$highestRow = $sheet-getHighestRow(); // 取得總行數(shù)

$highestColumn = $sheet-getHighestColumn(); // 取得總列數(shù)

$arr_result=array();

$strs=array();

for($j=2;$j=$highestRow;$j++)

{

unset($arr_result);

unset($strs);

for($k='A';$k= $highestColumn;$k++)

{

//讀取單元格

$arr_result .= $objPHPExcel-getActiveSheet()-getCell("$k$j")-getValue().',';

}

$strs=explode(",",$arr_result);

$sql="insert into php_excel(gid,stu_no,name,age) values ($strs[0],'$strs[1]','$strs[2]',$strs[3])";

echo $sql."br/";

mysql_query("set names utf8");

$result=mysql_query($sql) or die("執(zhí)行錯(cuò)誤");

$insert_num=mysql_affected_rows();

if($insert_num0){

$succ_result+=1;

}else{

$error_result+=1;

}

}

echo "插入成功".$succ_result."條數(shù)據(jù)?。?!br";

echo "插入失敗".$error_result."條數(shù)據(jù)?。?!";

其中conn.php代碼如下:

[php] view plain copy print?

$mysql=mysql_connect("localhost","root","") or die("數(shù)據(jù)庫(kù)連接失敗!");

mysql_select_db("php_excel",$mysql);

mysql_query("set names utf8");

我的導(dǎo)入效果如下:

至此,從Excel文件讀取數(shù)據(jù)批量導(dǎo)入到Mysql數(shù)據(jù)庫(kù)完成。

php怎么把數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)

需要PHP基礎(chǔ)知識(shí)和數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)。

以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫(kù)。

mysql_connect() 函數(shù)打開(kāi)MySQL 連接。舉例

?php

$con = mysql_connect("localhost","mysql_user","mysql_pwd");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}// 一些代碼...mysql_close($con);

?

mysql_connect()三個(gè)參數(shù)分別是服務(wù)器名,連接賬號(hào),連接密碼。

連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫(kù),后面則是用數(shù)據(jù)庫(kù)語(yǔ)句處理數(shù)據(jù)。SQL語(yǔ)法簡(jiǎn)介網(wǎng)頁(yè)鏈接

PHP網(wǎng)站怎么連接到數(shù)據(jù)庫(kù)?

常規(guī)方式

常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。

// 讀取配置文件內(nèi)容

$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123

PHP解析XML

上述兩種讀取文件,其實(shí)都是為了PHP解析XML來(lái)做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對(duì)于比較小型的xml配置文件,simplexml就足夠了。

配置文件

?xml version="1.0" encoding="UTF-8" ?mysql

!-- 為防止出現(xiàn)意外,請(qǐng)按照此標(biāo)準(zhǔn)順序書(shū)寫.其實(shí)也無(wú)所謂了 --

hostlocalhost/host

userroot/user

password123456/password

dbtest/db

port3306/port/mysql12345678910

解析

?php/**

* 作為解析XML配置文件必備工具

*/class XMLUtil {

public static $dbconfigpath = "./db.config.xml"; ? ?public static function getDBConfiguration() {

$dbconfig = array (); ? ? ? ?try { ? ? ? ? ? ?// 讀取配置文件內(nèi)容

$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節(jié)點(diǎn),進(jìn)而獲取相關(guān)的數(shù)據(jù)庫(kù)信息

$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點(diǎn)信息賦值給關(guān)聯(lián)數(shù)組,方便接下來(lái)的方法調(diào)用

$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關(guān)聯(lián)數(shù)組的形式返回

return $dbconfig;

} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數(shù)據(jù)庫(kù)配置文件信息出錯(cuò)!/markbr /" );

} ? ? ? ?return $dbconfig;

}

}1234567891011121314151617181920212223242526272829

數(shù)據(jù)庫(kù)連接池

對(duì)于PHP程序而言,優(yōu)化永無(wú)止境。而數(shù)據(jù)庫(kù)連接池就在一定程度上起到了優(yōu)化的作用。其使得對(duì)用戶的每一個(gè)請(qǐng)求而言,無(wú)需每次都像數(shù)據(jù)庫(kù)申請(qǐng)鏈接資源。而是通過(guò)已存在的數(shù)據(jù)庫(kù)連接池中的鏈接來(lái)返回,從時(shí)間上,效率上,都是一個(gè)大大的提升。

于是,這里簡(jiǎn)單的模擬了一下數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)。核心在于維護(hù)一個(gè)“池”。

從池子中取,用畢,歸還給池子。

?php/**x

* ?PHP中的數(shù)據(jù)庫(kù) 工具類設(shè)計(jì)

* ?郭璞

* ?2016年12月23日

*

**/class DbHelper { ? ?private $dbconfig; ? ?private $dbpool; ? ?public $poolsize; ? ?public function __construct($poolsize = 20) { ? ? ? ?if (! file_exists ( "./utils.php" )) { ? ? ? ? ? ?throw new RuntimeException ( "markutils.php文件丟失,無(wú)法進(jìn)行配置文件的初始化操作!/markbr /" );

}else {

require './utils.php';

} ? ? ? ?// 初始化 配置文件信息

$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫(kù)連接池“偽隊(duì)列”

$this-poolsize = $poolsize;

$this-dbpool = array (); ? ? ? ?for($index = 1; $index = $this-poolsize; $index ++) {

$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark連接數(shù)據(jù)庫(kù)失??!/markbr /" );

array_push ( $this-dbpool, $conn );

}

} ? ?/**

* 從數(shù)據(jù)庫(kù)連接池中獲取一個(gè)數(shù)據(jù)庫(kù)鏈接資源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池中已無(wú)鏈接資源,請(qǐng)稍后重試!/mark" );

} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );

}

} ? ?/**

* 將用完的數(shù)據(jù)庫(kù)鏈接資源放回到數(shù)據(jù)庫(kù)連接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池已滿/markbr /" );

} else {

array_push ( $this-dbpool, $conn );

}

}

}

怎么把php源碼數(shù)據(jù)庫(kù)導(dǎo)入數(shù)據(jù)庫(kù)

可以參考:

一般是單獨(dú)導(dǎo)入的,

在mysql上,要用mysql_import工具 把文本導(dǎo)入

sqlserver上可以用數(shù)據(jù)庫(kù)備份工具恢復(fù)導(dǎo)入, 也可以使用其他數(shù)據(jù)庫(kù)引擎通過(guò)ado到。

不需要源碼,但是需要了解php源碼所需要的庫(kù)表結(jié)構(gòu)。一般php源碼里好多都有建立空庫(kù)結(jié)構(gòu)的源碼。

當(dāng)前文章:php代碼咋導(dǎo)入數(shù)據(jù)庫(kù) php導(dǎo)入php文件
地址分享:http://www.bm7419.com/article32/ddehhpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作定制開(kāi)發(fā)、網(wǎng)站營(yíng)銷小程序開(kāi)發(fā)、自適應(yīng)網(wǎng)站服務(wù)器托管

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司