[英]php code to generate text files that fetches contents from a mysql table
I have written a php code that creates a text file and stores the contents of a myssql table in a row by row basis. 我写了一个php代码,该文件创建一个文本文件并逐行存储myssql表的内容。 The code i wrote is
我写的代码是
<?php
session_start();
include "functions.php";
db_connect();
$winners=mysql_query("SELECT * FROM winners") or die(mysql_error());
//creates a .txt file
$fp = fopen("myText.txt","wb");
while($winner=mysql_fetch_array($winners))
{
$event=$winner['Event'];
$position=$winner['Position'];
$name=$winner['Name'];
$college=$winner['College'];
$current = "$position $event $name $college";
file_put_contents($fp,$current,FILE_APPEND);
}
fclose($fp);
?>
The o/pi am getting is a blank text file with no contents. o / pi正在获取的是一个没有内容的空白文本文件。
The o/pi am expecting is a text file with the contents like 期望的o / pi是一个文本文件,其内容类似于
1 Sam SUDOKU MIT
2 David BASKETBALL OXFORD
I am a beginner in php and i'm not able to find out the problem with my piece of code??? 我是php的初学者,我无法通过我的代码找出问题所在???
file_put_contents uses a string as the first parameter, not a file pointer. file_put_contents使用字符串作为第一个参数,而不是文件指针。
Use fwrite instead. 请改用fwrite。
while($winner=mysql_fetch_assoc($winners))
{
$event=$winner['Event'];
$position=$winner['Position'];
$name=$winner['Name'];
$college=$winner['College'];
$current = "$position $event $name $college";
fwrite($fp,$current );
}
I figured out the code. 我弄清楚了代码。 The new code i wrote is:
我写的新代码是:
$winners=mysql_query("SELECT * FROM winners") or die(mysql_error());
//creates a .txt file
$fp = fopen("myText.txt","a+");
$top = 'Position' . "\t" . 'Event' . "\t" . 'Name' . "\t" . 'College' . "\n";
fwrite($fp,$top);
while($winner=mysql_fetch_array($winners))
{
$event=$winner['Event'];
$position=$winner['Position'];
$name=$winner['Name'];
$college=$winner['College'];
$contents="$position" . "\t" . "$event" . "\t" . "$name" . "\t" . "$college" . "\n";
fwrite($fp,$contents);
}
fclose($fp);
and the o/pi got is 而得到的o / pi是
Position Name Event College
1 Sam SUDOKU MIT
2 David BASKETBALL OXFORD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.