比如我已经有了123.sql文件
现在想弄一个安装页面,点提交就将123.sql导入到指定的表中
代码怎么写?
------解决方案--------------------
query(文件内容);
------解决方案--------------------
- PHP code
<?php if(...){//判断提交 //连接数据库... $path="./123.sql";//123.sql相对该php文件地址 mysql_query("source $path"); }
------解决方案--------------------
------解决方案--------------------
真的可以这样做吗?
不要自欺欺人了
------解决方案--------------------
找一个安装程序的开源项目,安装一下不就知道了。
dedecms的,一看就明了
大概如下
sql-dftables.txt
DROP TABLE IF EXISTS `#@__addonarticle`;
CREATE TABLE `#@__addonarticle` (
`aid` mediumint(8) unsigned NOT NULL default '0',
`typeid` smallint(5) unsigned NOT NULL default '0',
`body` mediumtext,
`redirecturl` varchar(255) NOT NULL default '',
`templet` varchar(30) NOT NULL default '',
`userip` char(15) NOT NULL default '',
PRIMARY KEY (`aid`),
KEY `typeid` (`typeid`)
) TYPE=MyISAM;
DROP TABLE IF EXISTS `#@__addonimages`;
CREATE TABLE `#@__addonimages` (
`aid` mediumint(8) unsigned NOT NULL default '0',
`typeid` smallint(5) unsigned NOT NULL default '0',
`pagestyle` smallint(6) NOT NULL default '1',
`maxwidth` smallint(6) NOT NULL default '600',
`imgurls` text,
`row` smallint(6) NOT NULL default '0',
`col` smallint(6) NOT NULL default '0',
`isrm` smallint(6) NOT NULL default '0',
`ddmaxwidth` smallint(6) NOT NULL default '200',
`pagepicnum` smallint(6) NOT NULL default '12',
`templet` varchar(30) NOT NULL default '',
`userip` char(15) NOT NULL default '',
`redirecturl` varchar(255) NOT NULL default '',
`body` mediumtext,
PRIMARY KEY (`aid`),
KEY `imagesMain` (`typeid`)
) TYPE=MyISAM;
DROP TABLE IF EXISTS `#@__addoninfos`;
CREATE TABLE `#@__addoninfos` (
`aid` int(11) NOT NULL default '0',
`typeid` int(11) NOT NULL default '0',
`channel` smallint(6) NOT NULL default '0',
`arcrank` smallint(6) NOT NULL default '0',
`mid` mediumint(8) unsigned NOT NULL default '0',
`click` int(10) unsigned NOT NULL default '0',
`title` varchar(60) NOT NULL default '',
`litpic` varchar(60) NOT NULL default '',
`userip` varchar(15) NOT NULL default ' ',
`senddate` int(11) NOT NULL default '0',
`flag` set('c','h','p','f','s','j','a','b') default NULL,
`lastpost` int(10) unsigned NOT NULL default '0',
`scores` mediumint(8) NOT NULL default '0',
`goodpost` mediumint(8) unsigned NOT NULL default '0',
`badpost` mediumint(8) unsigned NOT NULL default '0',
`nativeplace` smallint(5) unsigned NOT NULL default '0',
`infotype` smallint(5) unsigned NOT NULL default '0',
`body` mediumtext,
`endtime` int(11) NOT NULL default '0',
`tel` varchar(50) NOT NULL default '',
`email` varchar(50) NOT NULL default '',
`address` varchar(100) NOT NULL default '',
`linkman` varchar(50) NOT NULL default '',
PRIMARY KEY (`aid`),
KEY `typeid` (`typeid`,`nativeplace`,`infotype`),
KEY `channel` (`channel`,`arcrank`,`mid`,`click`,`title`,`litpic`,`senddate`,`flag`,`endtime`)
) TYPE=MyISAM;
。。。。
- PHP code
$query = ''; $fp = fopen(dirname(__FILE__).'/sql-dftables.txt','r'); while(!feof($fp)) { $line = rtrim(fgets($fp,1024)); if(ereg(";$",$line)) { $query .= $line."\n"; $query = str_replace('#@__',$dbprefix,$query); if($mysqlVersion < 4.1) { $rs = mysql_query($query,$conn); } else { if(eregi('CREATE',$query)) { $rs = mysql_query(eregi_replace('TYPE=MyISAM',$sql4tmp,$query),$conn); } else { $rs = mysql_query($query,$conn); } } $query=''; } else if(!ereg("^(//|--)",$line)) { $query .= $line; } } fclose($fp);