2012存储过程获取多结果集的详解_php技巧_脚本之
分类:www.澳门新萄京赌场

上一篇文章介绍了php-excel-reader读取excel文件的方法,因为需要,将excel这样的数据:

例1

PHPExcel是相当强大的 MS Office Excel 文档生成类库,当需要输出比较复杂格式数据的时候,PHPExcel 是个不错的选择。不过其使用方法相对来说也就有些繁琐

本代码是在thinkphp中开始使用的

复制代码 代码如下:$dbh = new PDO('sqlsrv:server=连接地址;Database=数据库名', 用户名, 密码);try {$procName = "P_Test_GetMixData";$stmt = $dbh->prepare("EXEC $procName ?, ?, ?");$nReturnValue = 0;$strReturnValue = "";$strSearchValue = "abandonship";$stmt->bindParam(1, $nReturnValue, PDO::PARAM_INT | PDO::PARAM_INPUT_OUTPUT, PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE);$stmt->bindParam(2, $strReturnValue, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 10);$stmt->bindParam(3, $strSearchValue , PDO::PARAM_STR);$stmt->execute();//获取第一个结果集.$rowset_1 = $stmt->fetch;print_r;echo '

新建数据库表如下:

代码示例

 代码如下

1.引入类

';//获取第二个结果集.$stmt->nextRowset();$rowset_2 = $stmt->fetch;echo '

-- 数据库: `alumni`

 代码如下

<?php
    set_time_limit(20000);
    ini_set('memory_limit','-1');
    require_once './PHPExcel.php';
    require_once './PHPExcel/IOFactory.php';
    require_once './PHPExcel/Reader/Excel5.php';
    
    //使用pdo连接数据库
    $dsn = "mysql:host=localhost;dbname=alumni;";
    $user = "root";
    $password = "";
    try{
    $dbh = new PDO($dsn,$user,$password);
    $dbh->query('set names utf8;');
    }catch(PDOException $e){
    echo "连接失败".$e->getMessage();
    }
    //pdo绑定参数操作
    $stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");
    $stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
    $stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
    $stmt->bindParam(":name", $name,PDO::PARAM_STR);
    
    $objReader = new PHPExcel_Reader_Excel5(); //use excel2007
    $objPHPExcel = $objReader->load('bks.xls'); //指定的文件
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数
    
    for($j=1;$j<=10;$j )
    {
    
    $student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列学号
    $name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名
    $gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid
    }
    //将获取的excel内容插入到数据库
    $stmt->execute();
?>

澳门新萄京, 代码如下

';$stmt->nextRowset();// 获取两个输出类型的参数echo $nReturnValue.'

-- 表的结构 `alumni`

require_once 'phpExcel/Classes/PHPExcel.php';
require_once 'phpexcel/Classes/PHPExcel/IOFactory.php';
require_once 'phpexcel/Classes/PHPExcel/Reader/Excel5.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($filename); //$filename可以是上传的文件,或者是指定的文件
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$k = 0;

Vendor('PHPExcel.PHPExcel');//引入扩展类.就是/
        Vendor('PHPExcel.PHPExcel.IOFactory');
        Vendor('PHPExcel.PHPExcel.Reader.Excel5');

'; echo $strReturnValue; } catch {echo $e->getMessage();}复制代码 代码如下:/***用于测试PDO调用MsSQLServer2012存储过程获取复合结果集Demo*Code CreateBy abandonship 2012.10.11**/CREATE PROCEDURE [dbo].[P_Test_GetMixData](@Message_1 tinyint output,@Messgae_2 varchar output,@SearchValue varchar Asset nocount onset @Message_1 = 123set @Messgae_2 = 'Hi,there!This is abandonship!'select * from _T1 where col1 like '%' @SearchValue '%'select * from _T2 where col1 like '%' @SearchValue '%'set nocount off:当bindParam中存在需要输出类型的参数时,必须包含长度:$length: An optional length of the data type. You can specify PDO::SQLSRV_PARAM_OUT_DEFAULT_SIZE to indicate the default size when using PDO::PARAM_INT or PDO::PARAM_BOOL in $data_type.

CREATE TABLE IF NOT EXISTS `alumni` (

//循环读取excel文件,读取一条,插入一条
for($j=2;$j<=$highestRow;$j )
{

 

`id` bigint NOT NULL AUTO_INCREMENT,

$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A列的值
$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B列的值
$sql = "INSERT INTO table VALUES(".$a.",".$b.")";
mysql_query($sql);

$excel_file= ROOT_PATH."/public/Uploads/".$publicity_bankdata_mod->where("id=".$data['id'])->getField('excel_file');
        //dump($excel_file);exit;
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($excel_file);//$uploadfile
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$arr_result = array();
$strs=array();
$strs_datas=array();
$succ_result=0;
$error_result=0;

`gid` varchar DEFAULT NULL COMMENT '档案编号',

}

上面看上去有点乱,下面我来写一个完整的类

`student_no` varchar DEFAULT NULL COMMENT '学号',

例2

 

`name` varchar DEFAULT NULL,

 代码如下

 代码如下

PRIMARY KEY ,

<?php
set_time_limit(20000);
ini_set('memory_limit','-1');
require_once './PHPExcel.php';
require_once './PHPExcel/IOFactory.php';
require_once './PHPExcel/Reader/Excel5.php';
 
//使用pdo连接数据库
$dsn = "mysql:host=localhost;dbname=alumni;";
$user = "root";
$password = "";
try{
 $dbh = new PDO($dsn,$user,$password);
 $dbh->query('set names utf8;');
}catch(PDOException $e){
 echo "连接失败".$e->getMessage();
}
//pdo绑定参数操作
$stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");
$stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
$stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
$stmt->bindParam(":name", $name,PDO::PARAM_STR);
 
$objReader = new PHPExcel_Reader_Excel5(); //use excel2007
$objPHPExcel = $objReader->load('bks.xls'); //指定的文件
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
 
for($j=1;$j<=10;$j )
{
 
$student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列学号
$name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名
$gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid
}
//将获取的excel内容插入到数据库
$stmt->execute();
?>

<?php
set_time_limit(20000);
ini_set('memory_limit','-1');
require_once './PHPExcel.php';
require_once './PHPExcel/IOFactory.php';
require_once './PHPExcel/Reader/Excel5.php';
 
//使用pdo连接数据库
$dsn = "mysql:host=localhost;dbname=alumni;";
$user = "root";
$password = "";
try{
 $dbh = new PDO($dsn,$user,$password);
 $dbh->query('set names utf8;');
}catch(PDOException $e){
 echo "连接失败".$e->getMessage();
}
//pdo绑定参数操作
$stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");
$stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
$stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
$stmt->bindParam(":name", $name,PDO::PARAM_STR);
 
$objReader = new PHPExcel_Reader_Excel5(); //use excel2007
$objPHPExcel = $objReader->load('bks.xls'); //指定的文件
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
 
for($j=1;$j<=10;$j )
{
 
$student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列学号
$name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名
$gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid
}
//将获取的excel内容插入到数据库
$stmt->execute();
?>

KEY `gid` ,

例3

php-excel-reader操作excel中的两个重要的方法:

KEY `name` ENGINE=MyISAM DEFAULT CHARSET=utf8;

新建数据库表如下:

1.dump(),它可以将excel内容以html格式输出:

导入后数据库结果如下:

 代码如下

echo $data->dump(true,true);

php源码如下:复制代码 代码如下: query; }catch{ echo "连接失败".$e->getMessage(); } //pdo绑定参数操作 $stmt = $dbh->prepare("insert into alumni values (:gid,:student_no,:name) "); $stmt->bindParam(":gid", $gid,PDO::PARAM_STR); $stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR); $stmt->bindParam(":name", $name,PDO::PARAM_STR); //使用php-excel-reader读取excel内容 $data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding; $data->read; for ($i = 1; $i <= $data->sheets[0]['numRows']; $i ) { for ($j = 1; $j <= 3; $j ) { $student_no = $data->sheets[0]['cells'][$i][1]; $name = $data->sheets[0]['cells'][$i][2]; $gid = $data->sheets[0]['cells'][$i][3]; } //将获取的excel内容插入到数据库 $stmt->execute(); } echo "执行成功"; echo "最后插入的ID:".$dbh->lastInsertId(); ?> 考虑到excel的量比较大,使用了PDO的绑定操作!

-- 数据库: `alumni`

2.将excel数据存入数组中,使用$data->sheets,打印下如下:

-- 表的结构 `alumni`

 代码如下

CREATE TABLE IF NOT EXISTS `alumni` (

 

`id` bigint(20) NOT NULL AUTO_INCREMENT,

Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 5
[numCols] => 4
[cells] => Array
(
[1] => Array
(
[1] => 编号
[2] => 姓名
[3] => 年龄
[4] => 学号
)
[2] => Array
(
[1] => 1
[2] => 小红
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => 小王
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => 小黑
[3] => 44
[4] => a1002
)
[5] => Array
(
[2] => by
[3] => www.phpddt.com
)
)
[cellsInfo] => Array
(
[1] => Array
(
[1] => Array
(
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[2] => Array
(
[1] => Array
(
[string] => 1
[raw] => 1
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 22
[raw] => 22
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[3] => Array
(
[1] => Array
(
[string] => 2
[raw] => 2
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[2] => Array
(
[xfIndex] => 23
)
[3] => Array
(
[string] => 33
[raw] => 33
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[4] => Array
(
[xfIndex] => 23
)
)
[4] => Array
(
[1] => Array
2012存储过程获取多结果集的详解_php技巧_脚本之家,PHPExcel读取excel并导入数据库。(
[string] => 3
[raw] => 3
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 44
[raw] => 44
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[5] => Array
(
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 24
[hyperlink] => Array
(
[flags] => 23
[desc] => www.111cn.net

`gid` varchar(20) DEFAULT NULL COMMENT '档案编号',

[link] =>
)
)
)
)
)
[1] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[2012存储过程获取多结果集的详解_php技巧_脚本之家,PHPExcel读取excel并导入数据库。numCols] => 0
)
[2] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
)

`student_no` varchar(20) DEFAULT NULL COMMENT '学号',

`name` varchar(32) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `gid` (`gid`),

KEY `name` (`name`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

php程序

 代码如下

<?php
header("Content-Type:text/html;charset=utf-8");
require_once 'excel_reader2.php';
set_time_limit(20000);
ini_set("memory_limit","2000M");
//使用pdo连接数据库
$dsn = "mysql:host=localhost;dbname=alumni;";
$user = "root";
$password = "";
try{
$dbh = new PDO($dsn,$user,$password);
$dbh->query('set names utf8;');
}catch(PDOException $e){
echo "连接失败".$e->getMessage();
}
//pdo绑定参数操作
$stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");
$stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
$stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
$stmt->bindParam(":name", $name,PDO::PARAM_STR);
//使用php-excel-reader读取excel内容
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('UTF-8');
$data->read("stu.xls");
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i ) {
for ($j = 1; $j <= 3; $j ) {
$student_no = $data->sheets[0]['cells'][$i][1];
$name = $data->sheets[0]['cells'][$i][2];
$gid = $data->sheets[0]['cells'][$i][3];
}
//将获取的excel内容插入到数据库
$stmt->execute();
}
echo "执行成功";
echo "最后插入的ID:".$dbh->lastInsertId();
?>

本文由澳门新萄京发布于www.澳门新萄京赌场,转载请注明出处:2012存储过程获取多结果集的详解_php技巧_脚本之

上一篇:澳门新萄京Excel类读取excel文件并且导入数据库, 下一篇:没有了
猜你喜欢
热门排行
精彩图文