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条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型