经测试代码如下:
/**
* 生成一条sql语句,用作MysqL的备份
*
* @param
* @arrange (512.笔记) jb51.cc
**/
if (!function_exists('MysqL_dump')) {
function MysqL_dump($database) {
$query = '';
$tables = @MysqL_list_tables($database);
while ($row = @MysqL_fetch_row($tables)) { $table_list[] = $row[0]; }
for ($i = 0; $i < @count($table_list); $i++) {
$results = MysqL_query('DESCRIBE ' . $database . '.' . $table_list[$i]);
$query .= 'DROP TABLE IF EXISTS `' . $database . '.' . $table_list[$i] . '`;' . lnbr;
$query .= lnbr . 'CREATE TABLE `' . $database . '.' . $table_list[$i] . '` (' . lnbr;
$tmp = '';
while ($row = @MysqL_fetch_assoc($results)) {
$query .= '`' . $row['Field'] . '` ' . $row['Type'];
if ($row['Null'] != 'YES') { $query .= ' NOT NULL'; }
if ($row['Default'] != '') { $query .= ' DEFAULT \'' . $row['Default'] . '\''; }
if ($row['Extra']) { $query .= ' ' . strtoupper($row['Extra']); }
if ($row['Key'] == 'PRI') { $tmp = 'primary key(' . $row['Field'] . ')'; }
$query .= ','. lnbr;
}
$query .= $tmp . lnbr . ');' . str_repeat(lnbr,2);
$results = MysqL_query('SELECT * FROM ' . $database . '.' . $table_list[$i]);
while ($row = @MysqL_fetch_assoc($results)) {
$query .= 'INSERT INTO `' . $database . '.' . $table_list[$i] .'` (';
$data = Array();
while (list($key,$value) = @each($row)) { $data['keys'][] = $key; $data['values'][] = addslashes($value); }
$query .= join($data['keys'],',') . ')' . lnbr . 'VALUES (\'' . join($data['values'],'\',\'') . '\');' . lnbr;
}
$query .= str_repeat(lnbr,2);
}
return $query;
}
}
/*** 来自编程之家 jb51.cc(jb51.cc) ***/