drxm5014 2016-04-03 21:12
浏览 77
已采纳

PDO Fetch_assoc返回单词“Array”

Im attempting to return a first name based off the users ID in a foreach statement but instead it returns the word "array" as the first name. I have no idea why its returning the word array in the scenario.

<table style="margin-top:-16px;">
                        <tbody style="overflow:hidden;">
                        <tr>
                            <th>Title</th>
                            <th>Author</th>
                            <th>Date Submitted</th>
                            <th>File Location</th>
                            <th>Download Status</th>
                            <th>Approve Status</th>
                        </tr>
                    <?php
                        $dbConnect = 'mysql:dbname=test;host=localhost';
                        $username = "test";
                        $password = "test";

                        try{
                            $dbConnect = new PDO($dbConnect, $username, $password);
                        } catch (PDOException $error) {
                            echo 'Error Connecting: ' . $error->getMessage();
                        }

                        $caseMgmtReturn = "SELECT * FROM `case`";

                        $caseMgmt = $dbConnect->query($caseMgmtReturn);

                        foreach ($caseMgmt as $row) {
                            $user = $row["userID"];
                            $firstPull = $dbConnect->prepare("SELECT `firstName` FROM `user` WHERE `userID`=:user");
                            $firstPull->bindValue(":user", $user);
                            $firstPull->execute();
                            $firstName = $firstPull->fetchAll(PDO::FETCH_ASSOC);

                            print "<tr> <td>" .$row["title"] . "</td> <td>" . $firstName . "</td> <td> " . $row["dateSubmitted"] . "</td> <td>" .  $row["fileLocation"] . "</td> <td>" .  $row["downloadStatus"] . "</td> <td>" .  $row["approvedStatus"] ."</td></tr><br/>";
                        }
                        ?>  
                        </tbody>  
                    </table>
  • 写回答

1条回答 默认 最新

  • douren8379 2016-04-03 21:22
    关注

    http://php.net/manual/en/pdostatement.fetchall.php

    If you are going to use ->fetchAll(PDO::FETCH_ASSOC) you need to then reference the associated Key=>Value pair, such as

    $firstName['firstName']
    

    If you use ->fetch() it returns an index based array, so you would retrieve your value like so;

    $firstName[0]
    

    You use index position 0 because in your SQL string you referenced a single column on the table, as 'firstName'. If you used

    SELECT * FROM
    

    Then you would need to know the order in which the columns are in the table which is what makes the ->fetchAll(PDO::FETCH_ASSSOC) so useful. You don't reference a number, you reference a column name.

    So your code should look like this:

    $firstPull = $dbConnect->prepare("SELECT `firstName` FROM `user` WHERE `userID`=:user");
    $firstPull->bindValue(":user", $user);
    $firstPull->execute();
    $values = $firstPull->fetchAll(PDO::FETCH_ASSOC);
    print "<tr> <td>" .$row["title"] . "</td> <td>" . $values['firstName'] . "</td> <td> " . $row["dateSubmitted"] . "</td> <td>" .  $row["fileLocation"] . "</td> <td>" .  $row["downloadStatus"] . "</td> <td>" .  $row["approvedStatus"] ."</td></tr><br/>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧