duanjiu1950 2015-09-03 15:06
浏览 15
已采纳

这个PHP代码有什么问题,有SQL查询吗?

Many days ago, I have read this question. Then, I have tried with this code:

$code = $_GET['code'];
$str_1 = mysql_query("SELECT * FROM `MyTable`");
while ($str_2 = mysql_fetch_array($str_1)) {
    header("content-type: application/octet-stream");
    header("content-disposition: inline; filename= " . $str_2['my_id'] . "");
    echo base64_decode($str_2['my_str']);
}

My my data base, there are some data:

+--------+-----------------+
|  my_id |     my_str      |
+--------+-----------------+
|  id_1  |  base64_str_1   |
|  id_2  |  base64_str_2   |
|  id_3  |  base64_str_3   |
|  id_4  |  base64_str_4   |
+--------+-----------------+

When I browse my PHP code with browser, such as: http://localhost/my_php.php?code=id_3. It always returns a file (named: id_3) which is contented (it's content: base64_str_1).

Can you tell me: "What is wrong in this PHP code, with SQL query?".

  • 写回答

1条回答 默认 最新

  • douqianke7467 2015-09-03 15:51
    关注

    With this code, You'll get the name of last row id from query. Just due to you replaced the header in:

    header("content-disposition: inline; filename= " . $str_2['my_id'] . "");
    

    The content, is being generated with a concatenation from all str rows. So, the generated content is:
    base64_str_1base64_str_2base64_str_3base64_str_4 and not only base64_str_1

    If you need to generate a unique file from one row from MyTable, you could to use:

    $code = $_GET['code'];
    $str_1 = mysql_query("SELECT * FROM `MyTable` where my_id = '$code'");
    header("content-type: application/octet-stream");
    header("content-disposition: inline; filename= " . mysql_result($str_1, 0, 'my_id') );
    echo base64_decode(mysql_result($str_1, 0, 'my_str'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信