function remove_res($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);
// 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);
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] = ""; }
假定以上sql指令保存于文件test.sql中$conn = mysql_connect(); //连接数据库$str = file_get_contents("test.sql"); //读取文件到变量$ar = split(";",$str); //分离各sql指令到数组foreach($ar as $sql) //循环执行sql指令
mysql_query($sql);
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;
mysql_query($sql);
if(! function_exists("file_get_contents")) {
function file_get_contents($filename) {
$tmp = file($filename);
return join("",$tmp);
}
}
{
$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语句的函数。
if(! function_exists("file_get_contents")) {
function file_get_contents($filename) {
$tmp = file($filename);
return join("",$tmp);
}
}