简体   繁体   English

记录不是来自 mysql 的文本文件

[英]Records are not coming in textfile from mysql

Here, I want to fetch record from mysql to textfile that I am writing into textfile.在这里,我想从 mysql 中获取记录到我正在写入文本文件的文本文件。
But textfile is creating as blank.但是文本文件正在创建为空白。
Where is the problem?问题出在哪儿?

My code:我的代码:

$myFile = 'abctest.txt';
//echo $myFile;exit;
$fo = fopen('com_order/order_textfile/'.$myFile, 'w+') or die("can't open file");

$data_query=mysql_query("SELECT order_id from tbl_order") or die(mysql_error());

while($data=mysql_fetch_array($data_query))
{
    $stringData = fwrite($fo, "Order: ".$data['order_id']."\n");
}
fwrite($fo, $stringData);
fclose($fo);

header('Content-type: text/plain');
header('Content-disposition: attachment; filename='.$myFile);

readfile() call is missing:缺少readfile()调用:

readfile($myFile);

So it should look like this:所以它应该是这样的:

header('Content-type: text/plain');
header('Content-disposition: attachment; filename='.$myFile);
readfile($myFile);

Here readfile() is the function which actually performs all the hard work (reads the file, writes to output).这里 readfile() 是实际执行所有艰苦工作的函数(读取文件,写入输出)。

More on the topic:有关该主题的更多信息:

readfile()读取文件()

Force download强制下载

UPDATE更新

Also you have a file path problem你也有文件路径问题

Note that you create file in com_order/order_textfile/'.$myFile but then use $myFile without the path.请注意,您在com_order/order_textfile/'.$myFile创建文件,然后使用没有路径的 $myFile 。 When referencing file you should use path instead:引用文件时,您应该使用路径:

$myFile = 'abctest.txt';
$filePath = 'com_order/order_textfile/'.$myFile;
$fo = fopen($filePath, 'w+') or die("can't open file");

$data_query=mysql_query("SELECT order_id from tbl_order") or die(mysql_error());

while($data=mysql_fetch_array($data_query))
{
    $stringData = fwrite($fo, "Order: ".$data['order_id']."\n");
}
fwrite($fo, $stringData);
fclose($fo);

header('Content-type: text/plain');
header('Content-disposition: attachment; filename='.$filePath);
readfile($filePath);

It seems like there is a problem with the way you execute your query but try the code below it's working.您执行查询的方式似乎存在问题,但请尝试使用下面的代码。

$link = mysqli_connect("localhost", "username", "password", "database name") or die("error unable to connect to database". mysqli_error($link)); 

$myFile = 'abctest.txt';
//echo $myFile;exit;
$fo = fopen('com_order/order_textfile/'.$myFile, 'w+') or die("can't open file");

$data_query=$link->query("SELECT order_id from tbl_order") or die(mysql_error());

while($data=mysqli_fetch_array($data_query))
{
    $stringData = fwrite($fo, "Order: ".$data['order_id']."\n");
    //echo("$stringData");
}
fwrite($fo, $stringData);
fclose($fo);

header('Content-type: text/plain');
header('Content-disposition: attachment; filename='.$myFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM