dongzang5815 2014-11-22 01:27
浏览 17
已采纳

创建mysql结果列表后强制下载.txt文件

I want to download a file which includes the results of my mysql query but my problem is, the .txt file includes only the last result of my mysql query. It should includes actually 5 results like this:

http://user:password@server.com:8080

Can someone show me where here the problem is?

<?php
        $result = mysql_query("SELECT serverurl FROM ibn");
    echo mysql_error();
    while ($row = mysql_fetch_array($result)) { 

    $result2 = mysql_query("SELECT streamport,streamname FROM streams");
    echo mysql_error();
    while ($row2 = mysql_fetch_array($result2)) { 

    $result3 = mysql_query("SELECT client_username,client_openpasswd FROM clients WHERE `client_id` = '$id' ");
    echo mysql_error();
    while ($row3 = mysql_fetch_array($result3)) { 

    $streamurl= $row['serverurl'];
    $streamport = $row2['streamport'];
    $streamuser= $row3['client_username'];
    $streampassword= $row3['client_openpasswd'];
    $streamchannel= $row2['streamname'];

    echo "<p>http://$streamuser:$streampassword@$streamurl:$streamport</p>";
                }   
            }

    }

//Generate text file on the fly

header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=bouquet.txt");

?>
  • 写回答

1条回答 默认 最新

  • duanqi6274 2014-11-22 03:57
    关注

    Your basic output can be achieved by moving your calls to header() to the top of the script and emitting your data later. The header commands will only be effective if they're issued before any output is sent to the client.

    You can demonstrate this with a simple script:

    <?php
    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=bouquet.txt");
    echo "A line of text
    ";
    

    The remainder of your code is more problematic. It's not clear why you're doing this, but the effect of your code is to produce the cartesian product of the three tables. i.e. every row in every table is combined with every row in every other table. I doubt this is actually what you want, but...

    You can achieve the same result more efficiently by using an SQL JOIN with no ON clause. Using that and concatenating the required fields you can do almost everything you want in a single SQL query:

    select
      concat('http://',
          clientname,':',
          clientopenpassword,'@',
          serverurl,':',
          streamsport)
      as URL 
    from ibn, streams, clients where client_id=1
    

    I'll leave converting this to a working PHP script as an exercise for the reader, but your code above provides a good template. Remember, only one query and one loop to fetch the result and emit it.

    Note that mysql_*() is deprecated - use mysqli_*() for new code. You should also watch for possible SQL injection where you use $id in your query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入