开发者

Why is PDOStatement->columnCount not returning the right number?

I'm querying the database first to get all records associated with a certain userid, then i need to go in and modify the array, because one of the fields is an id and i need the name associated with that id.

So i use columnCount to iterate through the resulting array at the index of the id and replace it with the right name, which works fine, for the first six results. columnCount only returns 6, but those first six are renamed as they should be. But outside of that it takes the results from that pdostatement and populates the table normally, with all the relevant data, 17 rows right now.

Why is it returning 6, or what am i doing to get that wrong columncount?

global $__CMS_CONN__;
    $timeqry = 'SELECT facility_id, program, date, visit_length, mileage, served FROM timesheet_db WHERE volunteer_id = '.$_SESSION['user_id'];
    $stmt = $__CMS_CONN__->prepare($timeqry);
    $stmt->execute();
    $columns = $stmt->columnCount();
 开发者_如何学Python   print $columns;
    if($stmt)
    {
        $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);

        for($x=0;$x<$stmt->columnCount();$x++)
        {
            global $__CMS_CONN__;
            $qry = 'SELECT facility FROM facility_db WHERE id = '.$arrValues[$x]['facility_id'];
            $stmt1 = $__CMS_CONN__->prepare($qry);
            $stmt1->execute();
            if($stmt1)
            {
                $facilityName = $stmt1->fetchAll(PDO::FETCH_ASSOC);
                foreach ($facilityName as $item)
                {
                    foreach ($item as $key => $val)
                    {
                        $arrValues[$x]['facility_id'] = $val;
                    }
                }
            }
        }
        print "<table style=\"font-size:90%\">\n";
        print "<tr>\n";
        print "<th style=\"width:100%\">Facility</th>";
        print "<th>Program</th>";
        print "<th>Date</th>";
        print "<th>Visit Length</th>";
        print "<th>Mileage</th>";
        print "<th>Served</th>";
        print "</tr>";
        foreach ($arrValues as $row)
        {
            print "<tr>";
            foreach ($row as $key => $val)
            {
                print "<td>$val</td>";
            }
            print "</tr>\n";
        }
        print "</table>\n";
    }


You asked for six columns in your first SELECT query:

SELECT facility_id, program, date, visit_length, mileage, served FROM timesheet_db WHERE volunteer_id = $_SESSION['user_id']

So, naturally, columnCount() is 6. It doesn't multiply the column count by the number of rows returned or anything like that.

If you want the number of rows, you need count($arrValues) (the manual says database behavior with rowCount() is inconsistent regarding SELECTs).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜