drh19790711 2014-04-07 09:01
浏览 56
已采纳

为什么只有从这个PHP数组返回的第一条记录?

I'm trying to return four records from my MySQL database but only the first record is being returned. I've searched but I'm unsure why this is. Can someone point me in the right direction?

    <?php
session_start();
function displayImage($username){
    $imageDate  = $_POST['imageDate'];
    $result = mysql_query("
        SELECT
            imageName
        FROM
            images AS i
        INNER JOIN
            users AS u ON i.userID = u.UserID
        WHERE
            u.username = '$username'
        AND
            i.imageDate = '$imageDate'

    ") or die(mysql_error());

    //return a message to the users explaining ......
        if (!isset($_POST['Submit'])) {
            // this does nowt yet!!!
            $output = "Nothing selected yet.";
        }
        else {
        //This is a while loop to store the SQL results into ......
        $row = array(mysql_fetch_assoc($result));
        foreach ($row as $picName) {
        $cam = $_POST['cam'];
        $fullPath = $username . "/" . $cam . "/" . $picName['imageName'];
        // $output = //this works fine
                reset($images);
            }
        }
        var_dump($row);
        echo "<br />";
        return $output;
    }
?>
  • 写回答

4条回答 默认 最新

  • doujianjian2060 2014-04-07 09:19
    关注

    As others here have said, you need to use a while loop for this, I've tidied the code up a tiny bit and added a couple of other things for you to consider.

    The actual reason for this is that when you use mysql_fetch_assoc it brings back one result resource and removes it from resources it has left to return. So if you just try and store it in an array, you get the first one in the array, and nothing else. When you use a while loop it works by basically saying "if mysql_fetch_assoc has something to give, then do the code inside the loop".

    <?php
    
    session_start();
    
    function displayImage($username) {
        // Also, as others have said, check out PDO, or MySQLi, as they
        // both provide a better interface with MySQL an have better
        // security features.
        $username  = mysql_real_escape_string($username);
        $imageDate = mysql_real_escape_string($_POST['imageDate']);
    
        $result = mysql_query("
            SELECT
                imageName
            FROM
                images AS i
            INNER JOIN
                users AS u ON i.userID = u.UserID
            WHERE
                u.username = '$username'
        AND 
                i.imageDate = '$imageDate'
        ") or die(mysql_error());
    
        //return a message to the users explaining
        if (!isset($_POST['Submit'])) {
            // this does nowt yet!!!
            $output = "Nothing selected yet.";
        } else {
            $cam = $_POST['cam'];
    
            $images = array(); // This is part of the "you could do something 
                               // like" further down.
            while ($row = mysql_fetch_assoc($result)) {
                $fullPath = $username . '/' . $cam . '/' . $row['imageName'];
                // $output = //this works fine
                var_dump($row);
                echo "<br />";
    
                // Or you could do something like:
    
                $images[] = $username . '/' . $cam . '/' . $row['imageName'];
    
                // Then outside of this while loop you'd have all of your image 
                // paths stored in this $images array. 
                // 
                // It depends on how you want to handle outputting them.
            }
        }
    
        return $output;
    }
    

    The comments in the code go through my main points.

    I also moved that $cam = $_POST['cam'] from inside of the loop, to outside of it, as it doesn't need to go in the loop because it's value will always be the same, regardless of which loop item you're going over.

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

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面