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;">
                            <th>Date Submitted</th>
                            <th>File Location</th>
                            <th>Download Status</th>
                            <th>Approve Status</th>
                        $dbConnect = 'mysql:dbname=test;host=localhost';
                        $username = "test";
                        $password = "test";

                            $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);
                            $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/>";
  • 写回答

1条回答 默认 最新

  • douren8379 2016-04-03 21:22


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


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


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


    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);
    $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 cpuid是如何转换的
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件