网站建设资讯

NEWS

网站建设资讯

php代码咋导入数据库 php导入php文件

如何用thinkphp实现将excel数据导入到mysql中

第一步:建立数据库和数据表(按照自己的Excel数据设立字段)。

创新互联是专业的凌源网站建设公司,凌源接单;提供网站设计制作、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行凌源网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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

第二步:前台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导入excel数据到MYSQL数据库/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

第三步:向数据库插入数据的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数据库

//调用phpexcel类库

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(); // 取得总行数

$highestColumn = $sheet-getHighestColumn(); // 取得总列数

$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("执行错误");

$insert_num=mysql_affected_rows();

if($insert_num0){

$succ_result+=1;

}else{

$error_result+=1;

}

}

echo "插入成功".$succ_result."条数据!!!br";

echo "插入失败".$error_result."条数据!!!";

其中conn.php代码如下:

[php] view plain copy print?

$mysql=mysql_connect("localhost","root","") or die("数据库连接失败!");

mysql_select_db("php_excel",$mysql);

mysql_query("set names utf8");

我的导入效果如下:

至此,从Excel文件读取数据批量导入到Mysql数据库完成。

php怎么把数据导入数据库

需要PHP基础知识和数据库基础知识。

以SQL为例。使用PHP MySQL 函数可以编辑数据库。

mysql_connect() 函数打开MySQL 连接。举例

?php

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

if (!$con)

{

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

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

?

mysql_connect()三个参数分别是服务器名,连接账号,连接密码。

连接之后,可以使用mysql_select_db()设置要处理的数据库,后面则是用数据库语句处理数据。SQL语法简介网页链接

PHP网站怎么连接到数据库?

常规方式

常规方式就是按部就班的读取文件了。其余的话和上述方案一致。

// 读取配置文件内容

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

PHP解析XML

上述两种读取文件,其实都是为了PHP解析XML来做准备的。关于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是对于比较小型的xml配置文件,simplexml就足够了。

配置文件

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

!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 --

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 {            // 读取配置文件内容

$handle = fopen(self::$dbconfigpath, "r");            $content = fread($handle, filesize(self::$dbconfigpath));            // 获取xml文档根节点,进而获取相关的数据库信息

$mysql = simplexml_load_string($content);            // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用

$dbconfig['host'] = $mysql-host;            $dbconfig['user'] = $mysql-user;            $dbconfig['password'] = $mysql-password;            $dbconfig['db'] = $mysql-db;            $dbconfig['port'] = $mysql-port;            // 将配置信息以关联数组的形式返回

return $dbconfig;

} catch ( Exception $e ) {            throw new RuntimeException ( "mark读取数据库配置文件信息出错!/markbr /" );

}        return $dbconfig;

}

}1234567891011121314151617181920212223242526272829

数据库连接池

对于PHP程序而言,优化永无止境。而数据库连接池就在一定程度上起到了优化的作用。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升。

于是,这里简单的模拟了一下数据库连接池的实现。核心在于维护一个“池”。

从池子中取,用毕,归还给池子。

?php/**x

*  PHP中的数据库 工具类设计

*  郭璞

*  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文件丢失,无法进行配置文件的初始化操作!/markbr /" );

}else {

require './utils.php';

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

$this-dbconfig = XMLUtil::getDBConfiguration ();        // 准备好数据库连接池“伪队列”

$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连接数据库失败!/markbr /" );

array_push ( $this-dbpool, $conn );

}

}    /**

* 从数据库连接池中获取一个数据库链接资源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() {        if (count ( $this-dbpool ) = 0) {            throw new ErrorException ( "mark数据库连接池中已无链接资源,请稍后重试!/mark" );

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

}

}    /**

* 将用完的数据库链接资源放回到数据库连接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) {        if (count ( $this-dbpool ) = $this-poolsize) {            throw new ErrorException ( "mark数据库连接池已满/markbr /" );

} else {

array_push ( $this-dbpool, $conn );

}

}

}

怎么把php源码数据库导入数据库

可以参考:

一般是单独导入的,

在mysql上,要用mysql_import工具 把文本导入

sqlserver上可以用数据库备份工具恢复导入, 也可以使用其他数据库引擎通过ado到。

不需要源码,但是需要了解php源码所需要的库表结构。一般php源码里好多都有建立空库结构的源码。


当前文章:php代码咋导入数据库 php导入php文件
网站路径:http://cdweb.net/article/ddehhpc.html