请问有人能解释一下如何使用PHP创建一个文本文件,并将记录从MySQL中导入到该文件中吗?
1)以写模式打开文件:
$myFile = "testFile.txt";
$fo = fopen($myFile, 'w') or die("can't open file");
2) 编写MySQL查询并获取其数据
$data_query=mysql_query("SELECT name,age from table");
while($data=mysql_fetch_array($data_query))
$stringData.="Name: ".$data['name']." Age:".$data['age']."\n";
3) 将数据写入文件
fwrite($fo, $stringData);
4) 关闭文件
fclose($fo);
$fh = fopen('games_set_88.php',"w");//php path
$extend_q = mysql_query('SELECT * from mod_games_set');
$row_count = mysql_num_rows($extend_q);
$col_count = mysql_num_rows(mysql_query('describe mod_games_set'));
$data_str = "<?php \n"." $"."mod_extend_type = array(\n";
$startout = 1;
while($row=mysql_fetch_assoc($extend_q)){
$startin = 1;
foreach($row as $key =>$value){
if( $startin<$col_count){
if($startin == 1){
$data_str .= " '".$value."'=>array('".$key."'=>'".$value."',";
}else{
if($startin < $col_count-1)
$data_str .= "'".$key."'=>'".$value."',";
else
$data_str .= "'".$key."'=>'".$value."')";
}
$startin++;
}
}
if($startout < $row_count) $data_str .= ",";
$data_str .= "\n";
$startout++;
}
$data_str .=" );\n?>";
fwrite($fh,$data_str);
fclose($fh);
file_put_contents
— 将字符串写入文件 - Gordon