doudiejian5827 2013-10-21 17:05
浏览 34
已采纳

使用php和mysql仅显示数据库中的内容一次

I'm trying to show images with a specific subject on the screen. I have 5 images with the same subject and it only shows one image. If I change the subject of the image in the database, the next image with the subject I try to call appears.

function selectSubject($subject){
        $showSubject = mysql_query("SELECT name FROM images WHERE subject = '$subject'");
        while($showSubject = mysql_fetch_array($showSubject)){
                $source_subject_trees = $showSubject['name'];
                echo "<img class=\"subject_images\" src='img/$source_subject_bomen'></>";
        }
    };
  • 写回答

2条回答 默认 最新

  • dongyu9894 2013-10-21 17:07
    关注

    mysql_query() returns a result handle. You then stomp all over that handle by re-assigning to it within your while() loop:

    $showSubject = mysql_query(...);
    
    while($showSubject = msyql_fetch_array($showSubject)) {
          ^^^^^^^^^^^^---here
    

    mysql_fetch_array() returns an array of one row's data. Since you're assigning to the SAME variable as you stored the actual query result handle, you destroy the result handle... and end up being unable to fetch any more data, because the handle's gone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码