如何用简单的php语句把*.sql导入数据库? 在线德行~~ 很急````

zhiin1 2004-08-11 05:27:00
如何用简单的php语句把下面的语句一次性导入数据库呢?

用phpmyadmin免谈!!

很急1 在线德行`` 谢谢````

CREATE DATABASE `lede`;
USE lede;


CREATE TABLE `admin` (
`user` varchar(30) NOT NULL default '',
`pass` varchar(50) NOT NULL default '',
PRIMARY KEY (`user`)
) TYPE=MyISAM;



INSERT INTO `admin` VALUES ('lede', 'lede');


CREATE TABLE `customer` (
`id` int(8) NOT NULL auto_increment,
`name` varchar(200) NOT NULL default '',
`time` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=50 ;



INSERT INTO `customer` VALUES (14, '中石化二建公司', '2004-08-07 08:13:43');
INSERT INTO `customer` VALUES (31, '中石化宁波工程建设公司', '2004-08-07 08:36:59');
INSERT INTO `customer` VALUES (15, '南京化工机械设备厂', '2004-08-07 08:23:11');
INSERT INTO `customer` VALUES (30, '安徽丰原生化有限公司', '2004-08-07 08:28:11');
INSERT INTO `customer` VALUES (32, '兰州铝业股份有限公司', '2004-08-07 08:23:43');
INSERT INTO `customer` VALUES (33, '青海格尔木炼油厂', '2004-08-07 08:28:55');
INSERT INTO `customer` VALUES (34, '中石化第五建设公司', '2004-08-07 08:19:55');
INSERT INTO `customer` VALUES (35, '上海赛科石化有限公司', '2004-08-07 08:24:15');
INSERT INTO `customer` VALUES (36, '青海桥头发电有限公司', '2004-08-07 08:29:27');
INSERT INTO `customer` VALUES (37, '中石化第十建设公司', '2004-08-07 08:20:37');
INSERT INTO `customer` VALUES (38, '上海邦凯塑胶有限公司', '2004-08-07 08:26:30');
INSERT INTO `customer` VALUES (39, '甘肃玉门炼油厂', '2004-08-07 08:30:01');
INSERT INTO `customer` VALUES (40, '中石化管道储运公司', '2004-08-07 08:21:37');
INSERT INTO `customer` VALUES (45, '大冶有色冶炼有限公司', '2004-08-07 08:32:32');
INSERT INTO `customer` VALUES (42, '上海化工设计院', '2004-08-07 08:27:11');
INSERT INTO `customer` VALUES (43, '浙江镇海石化有限公司', '2004-08-07 08:30:59');
INSERT INTO `customer` VALUES (44, '扬子石化公司', '2004-08-07 08:22:49');
INSERT INTO `customer` VALUES (46, '云南飞龙有色金属有限公司', '2004-08-07 08:33:14');
INSERT INTO `customer` VALUES (47, '江苏梅兰化工有限公司', '2004-08-07 08:34:19');
INSERT INTO `customer` VALUES (48, '江苏陵光石化有限公司', '2004-08-07 08:35:37');
INSERT INTO `customer` VALUES (49, '中石化洛阳工程公司', '2004-08-07 08:36:34');


CREATE TABLE `dynamic` (
`id` int(8) NOT NULL auto_increment,
`content` text NOT NULL,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;


INSERT INTO `dynamic` VALUES (1, '在稚鹰网际开发室的大力协助下,于2004年7月28日我公司的网站正式运行,大家有什么好的建议与意见可以通过留言板与管理员联系.', '2004-08-06 12:27:01');


CREATE TABLE `news` (
`id` int(8) NOT NULL auto_increment,
`title` varchar(200) NOT NULL default '',
`content` text NOT NULL,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=56 ;


INSERT INTO `news` VALUES (7, '乐得机械引进新设备~', '乐得机械将于近期引进国内最新设备\r\n具体情况等待发布', '2004-07-29 11:12:38');
INSERT INTO `news` VALUES (50, '乐得网站开通,欢迎大家测试', '乐得网站开通,欢迎大家测试,感谢大家支持!\r\n请记住本站域名 http://www.chinalede.com', '2004-07-29 11:11:07');
INSERT INTO `news` VALUES (52, '乐得网站开通,欢迎大家测试', '乐得网站开通,欢迎大家测试,感谢大家支持! 请记住本站域名 http://www.chinalede.com', '2004-07-29 11:21:17');
INSERT INTO `news` VALUES (53, '乐得网站开通,欢迎大家测试', '乐得网站开通,欢迎大家测试,感谢大家支持! 请记住本站域名 http://www.chinalede.com', '2004-07-29 11:21:56');
INSERT INTO `news` VALUES (54, '乐得网站开通,欢迎大家测试', '乐得网站开通,欢迎大家测试,感谢大家支持! 请记住本站域名 http://www.chinalede.com', '2004-07-29 11:22:12');
INSERT INTO `news` VALUES (55, '乐得网站开通,欢迎大家测试', '乐得网站开通,欢迎大家测试,感谢大家支持! 请记住本站域名 http://www.chinalede.com', '2004-07-29 11:22:28');


CREATE TABLE `order` (
`id` int(9) NOT NULL auto_increment,
`user` varchar(50) NOT NULL default '',
`sex` varchar(10) NOT NULL default '',
`computer` varchar(200) NOT NULL default '',
`address` varchar(250) NOT NULL default '',
`postcode` varchar(10) NOT NULL default '',
`tel` varchar(20) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`production` varchar(200) NOT NULL default '',
`number` varchar(40) NOT NULL default '',
`request` text NOT NULL,
`remark` text NOT NULL,
`time` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;" ;
...全文
413 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jaexc 2004-08-12
那就有表单?把*.sql文件内容提交到server上

$ar = split(";",$_POST['sqlstr']); //分离各sql指令到数组

foreach($ar as $sql) //循环执行sql指令
mysql_query($sql);
回复
wasg 2004-08-12
用discuz!论坛的安装程序里的方法就可以啊,我贴一段出来给你看.
function runquery($sql) {
global $tablepre, $db;

$sql = str_replace("\r", "\n", str_replace(' cdb_', ' '.$tablepre, $sql));
$ret = array();
$num = 0;
foreach(explode(";\n", trim($sql)) as $query) {
$queries = explode("\n", trim($query));
foreach($queries as $query) {
$ret[$num] .= $query[0] == '#' ? NULL : $query;
}
$num++;
}
unset($sql);

foreach($ret as $query) {
$query = trim($query);
if($query) {
if(substr($query, 0, 12) == 'CREATE TABLE') {
$name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
echo '建立数据表 '.$name.' ... <font color="#0000EE">成功</font><br>';
}
$db->query($query);
}
}
}

$sql = <<<EOT
DROP TABLE IF EXISTS cdb_announcements;
CREATE TABLE cdb_announcements (
id smallint(6) unsigned NOT NULL auto_increment,
author varchar(15) NOT NULL default '',
subject varchar(250) NOT NULL default '',
starttime int(10) unsigned NOT NULL default '0',
endtime int(10) unsigned NOT NULL default '0',
message text NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_buddys;
CREATE TABLE cdb_buddys (
username varchar(15) NOT NULL default '',
buddyname varchar(15) NOT NULL default ''
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_favorites;
CREATE TABLE cdb_favorites (
tid mediumint(8) unsigned NOT NULL default '0',
username varchar(15) NOT NULL default '',
KEY tid (tid)
) TYPE=MyISAM;

INSERT INTO cdb_forumlinks VALUES (1, 0, 'Discuz! Board', 'http://www.Discuz.net', '本站论坛程序 Discuz! 的官方站点,专门讨论 Discuz! 的使用与 Hack,提供论坛升级与技术支持等。', 'images/logo.gif');

INSERT INTO cdb_forums VALUES (1, 0, 'forum', '', '默认板块', '', 1, 0, '', 0, 0, 0, '', 1, 0, 1, 1, '', 0, '', '', '', '');

DROP TABLE IF EXISTS cdb_karmalog;
CREATE TABLE cdb_karmalog (
username varchar(15) NOT NULL default '',
pid int(10) unsigned NOT NULL default '0',
dateline int(10) unsigned NOT NULL default '0',
score tinyint(3) unsigned NOT NULL default '0'
) TYPE=MyISAM;

DROP TABLE IF EXISTS cdb_searchindex;
CREATE TABLE cdb_searchindex (
keywords varchar(200) NOT NULL default '',
results int(10) unsigned NOT NULL default '0',
dateline int(10) unsigned NOT NULL default '0',
KEY dateline (dateline)
) TYPE=MyISAM;

INSERT INTO cdb_settings VALUES ('Discuz! Board', 1, '', 1, 0, 0, '', 0, '', 0, '', 'Crossday Studio', 'http://www.crossday.com/', 'flat', 1, 1000, 15, 5, 10, 20, 10, 25, 10000, 0, 3, 0, 1, 10, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, '8', 'h:i A', 'Y-n-j', '2.0 <b style=\'color: #FF9900\'>COML</b>', '1 1040034649', 1, 'Crossday');

DROP TABLE IF EXISTS cdb_smilies;
CREATE TABLE cdb_smilies (
id smallint(6) unsigned NOT NULL auto_increment,
type enum('smiley','picon') NOT NULL default 'smiley',
code varchar(10) NOT NULL default '',
url varchar(30) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM;

INSERT INTO cdb_stylevars VALUES (1, 1, 'bgcolor', 'bg.gif');
INSERT INTO cdb_stylevars VALUES (2, 1, 'altbg1', '#F8F8F8');
INSERT INTO cdb_stylevars VALUES (3, 1, 'altbg2', '#FFFFFF');
INSERT INTO cdb_stylevars VALUES (4, 1, 'link', '#003366');
INSERT INTO cdb_stylevars VALUES (5, 1, 'bordercolor', '#000000');

EOT;
回复
pfc001 2004-08-12
是的file_get_contents函数只在php4.3.0以后才有
if(! function_exists("file_get_contents")) {
function file_get_contents($filename) {
$tmp = file($filename);
return join("",$tmp);
}
}
回复
zairwolfc 2004-08-12
同意 jaexc(大飞) ( ) 。



我做过安装程序,里面导入过。做法很简单,读取该sql内容,用;分行,然后在循环中mysql_query导入。
回复
skystar008 2004-08-12
function remove_remarks($sql)
{
$lines = explode("\n", $sql);

// try to keep mem. use down
$sql = "";

$linecount = count($lines);
$output = "";

for ($i = 0; $i < $linecount; $i++)
{
if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
{
if ($lines[$i][0] != "#")
{
$output .= $lines[$i] . "\n";
}
else
{
$output .= "\n";
}
// Trading a bit of speed for lower mem. use here.
$lines[$i] = "";
}
}

return $output;

}


function split_sql_file($sql, $delimiter)
{
// Split up our string into "possible" SQL statements.
$tokens = explode($delimiter, $sql);

// try to save mem.
$sql = "";
$output = array();

// we don't actually care about the matches preg gives us.
$matches = array();

// this is faster than calling count($oktens) every time thru the loop.
$token_count = count($tokens);
for ($i = 0; $i < $token_count; $i++)
{
// Don't wanna add an empty string as the last thing in the array.
if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0)))
{
// This is the total number of single quotes in the token.
$total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
// Counts single quotes that are preceded by an odd number of backslashes,
// which means they're escaped quotes.
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);

$unescaped_quotes = $total_quotes - $escaped_quotes;

// If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
if (($unescaped_quotes % 2) == 0)
{
// It's a complete sql statement.
$output[] = $tokens[$i];
// save memory.
$tokens[$i] = "";
}
else
{
// incomplete sql statement. keep adding tokens until we have a complete one.
// $temp will hold what we have so far.
$temp = $tokens[$i] . $delimiter;
// save memory..
$tokens[$i] = "";

// Do we have a complete statement yet?
$complete_stmt = false;

for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++)
{
// This is the total number of single quotes in the token.
$total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
// Counts single quotes that are preceded by an odd number of backslashes,
// which means they're escaped quotes.
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);

$unescaped_quotes = $total_quotes - $escaped_quotes;

if (($unescaped_quotes % 2) == 1)
{
// odd number of unescaped quotes. In combination with the previous incomplete
// statement(s), we now have a complete statement. (2 odds always make an even)
$output[] = $temp . $tokens[$j];

// save memory.
$tokens[$j] = "";
$temp = "";

// exit the loop.
$complete_stmt = true;
// make sure the outer loop continues at the right point.
$i = $j;
}
else
{
// even number of unescaped quotes. We still don't have a complete statement.
// (1 odd and 1 even always make an odd)
$temp .= $tokens[$j] . $delimiter;
// save memory.
$tokens[$j] = "";
}

} // for..
} // else
}
}

return $output;
}

然后用一个file框,可以上传文件。
然后用for 循环。楼主自己考虑一下吧。这些都是处理sql语句的函数。
回复
xuzuning 2004-08-12
是的file_get_contents函数只在php4.3.0以后才有
if(! function_exists("file_get_contents")) {
function file_get_contents($filename) {
$tmp = file($filename);
return join("",$tmp);
}
}
回复
fhiesc 2004-08-12
用phpmyadmin可以直接导入的.
回复
lzkd 2004-08-11
我用的办法大概是最笨的,不过,因为没别的办法,只能这么做.

就生成一个文本然后一条一条的insert
回复
zhiin1 2004-08-11
我用的是别人的服务器

不支持file_get_contents函数 ;(

还有其它办法吗?

phpmyadmin也不支持:(
回复
xuzuning 2004-08-11
php的数据库函数出于安全考虑不允许一次执行多条查询指令
假定以上sql指令保存于文件test.sql中

$conn = mysql_connect(); //连接数据库

$str = file_get_contents("test.sql"); //读取文件到变量

$ar = split(";",$str); //分离各sql指令到数组

foreach($ar as $sql) //循环执行sql指令
mysql_query($sql);
回复
zhiin1 2004-08-11
补充一句:

上面的语句是从phpmyadmin中直接导出的~~

回复
相关推荐
发帖
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
帖子事件
创建了帖子
2004-08-11 05:27
社区公告
暂无公告