< ?
if($table=="")exit();
mysql_connect("localhost","name","password");
mysql_select_db("database");
$result = mysql_query("select * from $table");
if(mysql_num_rows($result) < = 0) exit();
echo "开始转换数据到文本...
";
$handle = fopen("$table.txt","w");
$numfields = mysql_num_fields($result);
fputs($handle,$numfields."rn");
for($k=0;$k< mysql_num_rows($result);$k )
{
$msg = mysql_fetch_row($result);
for($i=0;$i< $numfields;$i )
{
$msg[$i] = str_replace("rn","&&php2000mysqlreturn&&",$msg[$i]);
$msg[$i] = str_replace("n" ,"&&php2000mysqlreturn&&",$msg[$i]);
fputs($handle,$msg[$i]."rn");
}
fputs($handle,"------- php2000 dump data program V1.0 for MySQL --------rn");
}
fclose($handle);
echo "ok";
?
< ?
if($table=="")exit();
mysql_connect("localhost","name","password");
mysql_select_db("database");
$message = file("$table.txt");
echo $numfields = chop($message[0]);
for($k=1;$k< count($message);$k =($numfields 1))
{
$value="";
for ($i=$k;$i< ($k $numfields-1);$i )
{
$tmp = str_replace("&&php2000mysqlreturn&&","rn",chop($message[$i]));
$value .= "'".addslashes($tmp)."',";
}
$tmp = str_replace("&&php2000mysqlreturn&&","rn",chop($message[$k $numfields-1]));
$value .= "'".$tmp."'";
$query = "insert into $table values (".$value.")";
echo mysql_error();
mysql_query($query);
echo $k." ";
}
echo "ok";
?