简体   繁体   English

在PHP中使用fopen和fwrite时的资源ID#7

[英]Resource id #7 when using fopen and fwrite in PHP

I'm trying to create a download link in PHP so you can download the information in a table. 我试图在PHP中创建一个下载链接,以便您可以将信息下载到表中。 I just started writing it and have run into a snag. 我刚开始写它,却遇到了麻烦。 Here is what I have so far: 这是我到目前为止有:

 <?php
 $sql = "SELECT * FROM " . $survey . ";";
 $result = mysql_query($sql)
    or die(mysql_error());
 $row = mysql_fetch_assoc($result);


 $something = "This is text";
 $myFile = "data.txt";
 $fh = fopen($myFile, 'w') or die("can't open file");

 $download_data = "";
 foreach ($row as $k=>$v){
        $download_data .= $k . "=" . $v . "\n";
 }
 fwrite($fh, $download_data);
 fclose($fh);

 echo $download_data;

 ?>
 <a href="data.txt">Download </a>

It is just supposed to show something like Code = 1 Name = John etc. When I open the txt file, it simply says Resource id #7. 它只是应该显示类似Code = 1 Name = John等的内容。当我打开txt文件时,它只是说Resource ID#7。 The weird part is, when I echo $download_data, it looks correctly in the web page. 奇怪的是,当我回显$ download_data时,它在网页中看起来正确。 Is there something special I have to do with fwrite in order to get the whole string into the text file 为了使整个字符串进入文本文件,我需要对fwrite做一些特殊的事情

(Note: I have used both mysql_fetch_array and mysql_fetch_assoc and both have the same result. Also, if I simply declare a variable like $test = "this is a test"; it works). (注意:我同时使用了mysql_fetch_array和mysql_fetch_assoc,并且结果相同。此外,如果我简单地声明一个变量,例如$ test =“ this is a test”;它可以工作)。

Edit: I have tried commenting out all other code in the script and I get the same result. 编辑:我试图注释掉脚本中的所有其他代码,我得到相同的结果。 Printing $download_data shows the right result, but the text file is still only showing Resource id #7. 打印$ download_data显示正确的结果,但是文本文件仍然仅显示Resource ID#7。 I've even tried deleting the txt file and when it is recreated, it does the same thing. 我什至尝试删除txt文件,并在重新创建它时执行相同的操作。

You're almost certainly not showing us the whole picture with your example code. 您几乎可以肯定不会通过示例代码向我们展示整个图片。

You are likely writing one of these things to the file: 您可能会将以下其中一项写入文件:

  1. The file handle, $fh 文件句柄$fh
  2. The result returned from mysql_query() mysql_query()返回的结果

Check you're not mixing up $result and $row . 检查您是否没有混淆$result$row Or even better, post all of your code. 甚至更好,发布所有代码。

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

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