Warning: Cannot modify header information - headers already sent by

 

网上有很多解决方法,我说个我自己使用的解决方法:

 

http://hi.baidu.com/xiaefun/blog/item/87fd5f82e96a3eaa0df4d2cb.html

 

<?php /** * 读取 sql 文件并写入数据库 * @version 1.01 demo.php */ class DBManager { var $dbHost = ''; var $dbUser = ''; var $dbPassword = ''; var $dbSchema = ''; function __construct($host,$user,$password,$schema) { $this->dbHost = $host; $this->dbUser = $user; $this->dbPassword = $password; $this->dbSchema = $schema; } function createFromFile($sqlPath,$delimiter = '(;/n)|((;/r/n))|(;/r)',$prefix = '',$commenter = array('#','--')) { //判断文件是否存在 if(!file_exists($sqlPath)) return false; $handle = fopen($sqlPath,'rb'); $sqlStr = fread($handle,filesize($sqlPath)); //通过sql语法的语句分割符进行分割 $segment = explode(";",trim($sqlStr)); //var_dump($segment); //去掉注释和多余的空行 foreach($segment as & $statement) { $sentence = explode("/n",$statement); $newStatement = array(); foreach($sentence as $subSentence) { if('' != trim($subSentence)) { //判断是会否是注释 $isComment = false; foreach($commenter as $comer) { if(eregi("^(".$comer.")",trim($subSentence))) { $isComment = true; break; } } //如果不是注释,则认为是sql语句 if(!$isComment) $newStatement[] = $subSentence; } } $statement = $newStatement; } //对表名加前缀 if('' != $prefix) { //只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName $regxTable = "^[/`/'/"]{0,1}[/_a-zA-Z]+[/_a-zA-Z0-9]*[/`/'/"]{0,1}$";//处理表名的正则表达式 $regxLeftWall = "^[/`/'/"]{1}"; $sqlFlagTree = array( "CREATE" => array( "TABLE" => array( "$regxTable" => 0 ) ), "INSERT" => array( "INTO" => array( "$regxTable" => 0 ) ) ); foreach($segment as & $statement) { $tokens = split(" ",$statement[0]); $tableName = array(); $this->findTableName($sqlFlagTree,$tokens,0,$tableName); if(empty($tableName['leftWall'])) { $newTableName = $prefix.$tableName['name']; } else{ $newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1); } $statement[0] = str_replace($tableName['name'],$newTableName,$statement[0]); } } //组合sql语句 foreach($segment as & $statement) { $newStmt = ''; foreach($statement as $sentence) { $newStmt = $newStmt.trim($sentence)."/n"; } $statement = $newStmt; } //用于测试------------------------ // print_r($segment); // writeArrayToFile('data.txt',$segment); //------------------------------- self::saveByQuery($segment); return true; } private function saveByQuery($sqlArray) { $conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword); mysql_select_db($this->dbSchema); foreach($sqlArray as $sql) { mysql_query($sql); } mysql_close($conn); } private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,& $tableName = array()) { $regxLeftWall = "^[/`/'/"]{1}"; if(count($tokens)<=$tokensKey) return false; if('' == trim($tokens[$tokensKey])) { return self::findTableName($sqlFlagTree,$tokens,$tokensKey+1,$tableName); } else { foreach($sqlFlagTree as $flag => $v) { if(eregi($flag,$tokens[$tokensKey])) { if(0==$v) { $tableName['name'] = $tokens[$tokensKey]; if(eregi($regxLeftWall,$tableName['name'])) { $tableName['leftWall'] = $tableName['name']{0}; } return true; } else{ return self::findTableName($v,$tokens,$tokensKey+1,& $tableName); } } } } return false; } } function writeArrayToFile($fileName,$dataArray,$delimiter="/r/n") { $handle=fopen($fileName, "wb"); $text = ''; foreach($dataArray as $data) { $text = $text.$data.$delimiter; } fwrite($handle,$text); } /** * 如果已经安装过,该文件将存在 */ ob_start(); if (file_exists('dbExists.txt')) { // echo $_SERVER['REMOTE_HOST']; header('location: root.html'); } else { //测试 $dbM = new DBManager('localhost','root','','management'); $result = $dbM->createFromFile('management.sql',null,''); if ($result) { echo '数据库安装完成!'; $handle = fopen('dbExists.txt', 'w'); fwrite($handle, '1'); header('location: root.html'); } else { echo '数据库安装失败!请与徐杰联系,13588132669'; } } ob_end_flush(); ?>

 

这里可以用来操作所有通过phpMyAdmin的dump出来的sql文件,比如随便一个sql文件如下:

-- phpMyAdmin SQL Dump -- version 2.11.9.2 -- http://www.phpmyadmin.net -- -- 主机: 127.0.0.1:3306 -- 生成日期: 2010 年 11 月 16 日 14:11 -- 服务器版本: 5.1.28 -- PHP 版本: 5.2.6 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- 数据库: `management` -- CREATE DATABASE `management` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; USE `management`; -- -------------------------------------------------------- -- -- 表的结构 `fuliao` -- CREATE TABLE IF NOT EXISTS `fuliao` ( `id` mediumint(9) NOT NULL AUTO_INCREMENT COMMENT '自增', `name` varchar(50) NOT NULL COMMENT '样品名称', `type` varchar(50) NOT NULL COMMENT '辅料类型', `price` double unsigned NOT NULL COMMENT '样品价格', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 COMMENT='辅料' AUTO_INCREMENT=1 ; -- -- 导出表中的数据 `fuliao` -- -- -------------------------------------------------------- -- -- 表的结构 `in` -- CREATE TABLE IF NOT EXISTS `in` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增', `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '入库日期', `danhao` varchar(20) NOT NULL COMMENT '单号', `address` varchar(200) NOT NULL COMMENT '地址', `name` varchar(10) NOT NULL, `phone` char(11) NOT NULL COMMENT '手机号码', `sampleName` varchar(20) NOT NULL COMMENT '样品名称', `sampleType` varchar(50) NOT NULL COMMENT '样品型号', `price` double NOT NULL COMMENT '样品价格', `sampleNum` smallint(5) NOT NULL COMMENT '样品数量', `totalMoney` double NOT NULL COMMENT '总额', `ywy` varchar(10) NOT NULL COMMENT '业务员名字', `desc` varchar(200) NOT NULL COMMENT '说明', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=1 ; -- -- 导出表中的数据 `in` -- -- -------------------------------------------------------- -- -- 表的结构 `out` -- CREATE TABLE IF NOT EXISTS `out` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增', `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '入库日期', `danhao` varchar(20) NOT NULL COMMENT '单号', `address` varchar(200) NOT NULL COMMENT '地址', `name` varchar(10) NOT NULL, `phone` char(11) NOT NULL COMMENT '手机号码', `sampleType` varchar(50) NOT NULL COMMENT '样品型号', `price` double NOT NULL COMMENT '样品价格', `sampleNum` smallint(5) NOT NULL COMMENT '样品数量', `totalMoney` double NOT NULL COMMENT '总额', `ywy` varchar(10) NOT NULL COMMENT '业务员名字', `desc` varchar(200) NOT NULL COMMENT '说明', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=1 ; -- -- 导出表中的数据 `out` -- -- -------------------------------------------------------- -- -- 表的结构 `sample` -- CREATE TABLE IF NOT EXISTS `sample` ( `id` mediumint(9) NOT NULL AUTO_INCREMENT COMMENT '自增', `name` varchar(50) NOT NULL COMMENT '样品名称', `type` varchar(50) NOT NULL COMMENT '样品型号', `price` double unsigned NOT NULL COMMENT '样品价格', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 COMMENT='样品' AUTO_INCREMENT=1 ; -- -- 导出表中的数据 `sample` -- -- -------------------------------------------------------- -- -- 表的结构 `ywy` -- CREATE TABLE IF NOT EXISTS `ywy` ( `id` smallint(6) NOT NULL AUTO_INCREMENT COMMENT '自增', `name` varchar(10) NOT NULL COMMENT '业务员名字', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 COMMENT='业务员' AUTO_INCREMENT=1 ; -- -- 导出表中的数据 `ywy` --

 

就可以把真个sql文件,一次性的导入到数据库中,非常方便。

 

你可能感兴趣的:(sql,header,phpMyAdmin,null,character,collation)