呵呵,那你看一下phpmyadmin这么写的不就知道了?
解决方案 »
- 我这个文章列表页不知道为什么运行之后cpu就100%,请教高手!
- php写WebService无法通信
- 求教:公告板通过简要链到全文的sql语句,请大侠帮忙修改:)
- 一个从来没有碰过的运行错误,求教~~
- 高手们请指点,小弟多谢啦:)
- 传递变量有疑
- 如何使用PHP操纵FTP服务器的用户及其权限?
- 菜鸟的问题:关于Session怎么读入怎么写,怎么进行过期时间限制?
- 100分寻求session的解决方法
- 那里可以找到WIN2000下PHP 对 SQL Server 2000 的支持的动态链接库??
- 有没有办法让PHP程序在windows服务里运行
- 关于在smarty 模板里的JavaScript脚本变量接受附值问题
win下写个批处理文件dump.bat
linux下写个shell文件dump.sh
都是用来执行mysqldump命令,然后在php中用system函数调用这些文件。
2、读取DB的所有表,再逐个fetcht各表内的数据
3、循环输出sql语句;
<pre>
<?PHP$conn = mysql_connect("localhost","root","");
mysql_select_db('mysql');
$res = mysql_query('show tables;');
while($row[]=mysql_fetch_array($res,MYSQL_NUM));
array_pop($row);
print_r($row);
for ($i=0;$i<count($row);$i++){
$tmpRes = mysql_query("SHOW CREATE TABLE {$row[$i][0]}");
$createSql = mysql_fetch_array($tmpRes,MYSQL_NUM);
$tmpTable = $row[$i][0];
$sql = "select * from $tmpTable";
$tmpRes = mysql_query($sql);
while($dataRow[]=mysql_fetch_array($tmpRes,MYSQL_ASSOC)){}
array_pop($dataRow);
print_r($createSql);
echo "<hr>";
print_r($dataRow);
}
?>
</pre>
function mysqldump() {
global $mysql_db;
$crlf = "\n";
$drop = 0;
$cmdEnd = ";";
$tables = mysql_list_tables($mysql_db);
$num_tables = @mysql_numrows($tables);
if($num_tables == 0){
echo " No Table Found .";
}
else {
$str = "/* Dump Mysql Database Into Sybase Database Format */".$crlf;
$str.= "/* From Mysql Database : $mysql_db */".$crlf;
$str.= "/* DateTime : ".date("Y-m-d h:i:s")." */".$crlf;
echo $str;
}
$i=0;
while ($i < $num_tables) {
$table = mysql_tablename($tables, $i++);
$str = $crlf;
$str.= "/*---------------------------------------------------------*/".$crlf;
$str.= "/* Table Structure for '$table' */".$crlf;
$str.= "/*---------------------------------------------------------*/".$crlf;
$str.= $crlf;
echo $str;
$tb = $this->get_table_def($table) ; $tb_def = $tb[0];
echo $tb_def;
$tb_col = $tb[1];
$tb_identity = $tb[2];
$this->get_table_content($table,$tb_col,$tb_identity);
}
}
$crlf = "\n";
$drop = 0;
$cmdEnd = ";";
$schema_create = "";
$identity = '';
if(!empty($drop)){
$schema_create .= "DROP TABLE $table $crlf ".$cmdEnd." $crlf ";
}
$schema_create .= "CREATE TABLE $table ($crlf";
$result = mysql_query("SHOW FIELDS FROM $table") or mysql_die(); while ($row = mysql_fetch_array($result)) {
$schema_create .= " $row[Field] $row[Type]"; $col_def[$row[Field]] = $row[Type]; if($row["Null"] == "YES"){ // mysql
$schema_create .= " NULL ";
}
else{
$schema_create .= " NOT NULL ";
}
if(!empty($row["Default"])) {
$schema_create .= " DEFAULT '$row[Default]' ";
}
if($row["Extra"] != ""){
$identity = $row[Field];
$schema_create .= " $row[Extra]";
} $schema_create .= ",$crlf";
}
$schema_create = ereg_replace(",".$crlf."$", "", $schema_create); $result = mysql_query("SHOW KEYS FROM $table") or mysql_die(); while ($row = mysql_fetch_array($result)){
$kname=$row['Key_name'];
if(($kname != "PRIMARY") && ($row['Non_unique'] == 0))
$kname="UNIQUE|$kname";
if(!is_array($index[$kname])){
$index[$kname] = array();
}
$index[$kname][] = $row['Column_name'];
}
while(list($x, $columns) = @each($index)){
if($x == "PRIMARY"){
$schema_create .= ",$crlf";
$schema_create .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
}
else if (substr($x,0,6) == "UNIQUE") {
$schema_create .= ",$crlf";
$schema_create .= " UNIQUE (" . implode($columns, ", ") . ")";
}
else {
$schema_index .= " create index i_$x on $table (" . implode($columns, ", ") . ")".$crlf.$cmdEnd.$crlf;
}
}
$schema_create .= "$crlf)".$crlf.$cmdEnd.$crlf;
$schema_create .=$schema_index ;
return array(stripslashes($schema_create),$col_def,$identity);
}
$drop = 0;
$cmdEnd = ";";
$crlf="\n";
$i = 0;
$cols = "";
//column name;
$cols = "(";
while(list($key,$val)=@each($col_def)) {
$cols.=$key.",";
}
$cols = substr($cols,0,strlen($cols)-1).")";
reset($col_def); $str = $crlf;
$str.= "/*---------------------------------------------------------*/".$crlf;
$str.= "/* Dumping data for table '$table' */".$crlf;
$str.= "/*---------------------------------------------------------*/".$crlf;
$str.= $crlf;
echo $str; $result = mysql_query("SELECT * FROM $table") or mysql_die(); while ($row = mysql_fetch_array($result)) {
$schema_insert = "INSERT INTO $table $cols VALUES("; while(list($key,$val)=each($col_def)) {
if(!isset($row[$key])) {
$schema_insert .= " NULL,";
}
elseif ($row[$key] != "") {
/* ¼Æ­ÈÃþ«¬¤£¯à¥Î¤Þ¸¹ */
$sub_val = substr($val,0,4);
if($val=='int' || $val=='tinyint' || $val=='integer' || $val=='smallint' ||
$sub_val=='nume' || $sub_val=='floa' || $sub_val=='deci' ||
$sub_val=='doub' || $sub_val=='real')
$schema_insert .= $row[$key].",";
else if($val=='date' && $row[$key]=="0000-00-00")
$schema_insert .= "'',";
else /* ¦pªGÄæ¦ì­È¥]§t¤Þ¸¹,¤£¬O¥Î\,¦Ó¬O±Ä¥Î ­«½Æ ªº¤è¦¡ */
$schema_insert .= " '".str_replace("\\n","\\\\n",$this->addQuote($row[$key]))."',";
// $schema_insert .= " '".$this->addQuote($row[$key])."',";
}
else {
$schema_insert .= " '',";
}
}
$schema_insert = ereg_replace(",$", "", $schema_insert);
$schema_insert .= ")";
$schema_insert .= $crlf.$cmdEnd.$crlf;
echo $schema_insert;
reset($col_def);
$i++;
} return (true);
}
function addQuote($str) {
$str = str_replace("'","''",$str);
return $str;
}